MongoDBでコレクションを作成した後、データを入力するには?
MongoDBでは、インサートまたはインサートManyコマンドを使用してコレクションにデータを挿入できます。
- 本質的に
db.collection.insert({ field1: value1, field2: value2, ... });
- 一括挿入
db.collection.insertMany([
{ field1: value1, field2: value2 },
{ field1: value3, field2: value4 },
...
]);
collectionはデータを追加するコレクションの名前。field1、field2などのfieldはドキュメントのフィールド、value1、value2などは対応するフィールドの値。
一例をあげると、
- 母国語の日本語で言い換えてください。1 つのオプションのみ必要です。
db.createCollection("users");
- 複数の選択肢は必要ありません。日本語で自然な文章に言い換えてください。
db.users.insert({ name: "Alice", age: 25 });
- ユーザーは、ネイティブの日本語で文章を言い換えることができます。
db.users.insertMany([
{ name: "Bob", age: 30 },
{ name: "Charlie", age: 35 }
]);
これらのコマンドを使用してMongoDBコレクションにデータを入力できます。