文档菜单
文档首页
/
MongoDB 手册
/

插入文档

在本页

  • 在 MongoDB Atlas UI 中插入文档
  • 插入单个文档
  • 插入多个文档
  • 插入行为

使用上角右侧的选择语言下拉菜单设置以下示例的语言或选择 MongoDB Compass。


本页提供了MongoDB中插入操作的示例。

您可以通过以下方法在MongoDB中插入文档:

  • 您编程语言的驱动程序。

  • 以下MongoDB Atlas UI。欲了解更多信息,请参阅MongoDB Atlas UI中的插入文档

  • MongoDB Compass.

注意

创建集合

如果集合当前不存在,插入操作将创建集合。

要在MongoDB Atlas UI中插入一个文档,请完成以下步骤。有关在MongoDB Atlas UI中处理文档的更多信息,请参阅创建、查看、更新和删除文档

1
  1. 如果它尚未显示,请从导航栏中的 组织菜单选择包含您所需项目的组织

  2. 如果它尚未显示,请从导航栏中的项目菜单选择您的项目。

  3. 如果它尚未显示,请点击侧边栏中的集群

    集群页面显示集群

2
  1. 对于您想添加文档的集群,单击浏览集合

  2. 在左侧导航窗格中,选择数据库。

  3. 在左侧导航窗格中,选择集合。

3
  1. 单击插入文档

  2. 单击{}图标,这将打开JSON视图。

  3. 将文档数组粘贴到文本输入字段中。例如,以下条目创建四个文档,每个文档包含三个字段

    [
    { "prodId": 100, "price": 20, "quantity": 125 },
    { "prodId": 101, "price": 10, "quantity": 234 },
    { "prodId": 102, "price": 15, "quantity": 432 },
    { "prodId": 103, "price": 17, "quantity": 320 }
    ]
4

MongoDB Atlas会将文档添加到集合中。

db.collection.insertOne() 将一个 单个 文档 插入到一个集合中。

以下示例将一个新文档插入到 inventory 集合中。如果文档没有指定 _id 字段,MongoDB 将添加一个包含 ObjectId 值的 _id 字段到新文档中。有关更多信息,请参阅 插入行为。

要使用 MongoDB Compass:

  1. 导航到您要插入文档的集合

    1. 在 MongoDB Compass 左侧导航面板中,点击您的目标集合所属的数据库。

    2. 从数据库视图,点击目标集合名称。

  2. 点击 插入文档 按钮

    Compass insert button
  3. 对于文档中的每个字段,选择字段类型并填写字段名称和值。通过单击最后一行数字,然后单击 在 ... 后添加字段 来添加字段。

    • 对于 Object 类型,通过单击最后一个字段的编号并选择 在 ... 后添加字段 来添加嵌套字段。

    • 对于 Array 类型,通过单击最后一个元素的行编号并选择 在 ... 后添加数组元素 来向数组添加更多元素。

  4. 填写完所有字段后,单击 插入

以下示例将新文档插入到 test.inventory 集合中

以下示例将新文档插入到 inventory 集合中。如果文档没有指定 _id 字段,C 驱动程序会向新文档中添加一个包含 ObjectId 值的 _id 字段。有关更多信息,请参阅 插入行为。

IMongoCollection.InsertOne() 将单个 文档 插入到集合中。

以下示例将新文档插入到 inventory 集合中。如果文档未指定 _id 字段,C# 驱动程序会自动添加一个包含 ObjectId 值的 _id 字段到新文档中。请参阅插入行为。

Collection.InsertOne 将单个 文档 插入到集合中。

以下示例将新文档插入到 inventory 集合中。如果文档未指定 _id 字段,驱动程序会自动添加一个包含 ObjectId 值的 _id 字段到新文档中。请参阅插入行为。

com.mongodb.reactivestreams.client.MongoCollection.insertOne 将一个 单个 文档 插入到使用 Java Reactive Streams Driver 的集合中

{ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }

以下示例将上面的文档插入到 inventory 集合中。如果文档没有指定 _id 字段,驱动程序会向新文档添加一个包含 ObjectId 值的 _id 字段。请参阅 插入行为。

com.mongodb.client.MongoCollection.insertOne 将一个 单个 文档 插入到集合中。

以下示例将新文档插入到 inventory 集合中。如果文档未指定 _id 字段,驱动程序会自动添加一个包含 ObjectId 值的 _id 字段到新文档中。请参阅插入行为。

MongoCollection.insertOne 向集合中插入一个 单个 文档

以下示例将新文档插入到 inventory 集合中。如果文档未指定 _id 字段,驱动程序会自动添加一个包含 ObjectId 值的 _id 字段到新文档中。请参阅插入行为。

motor.motor_asyncio.AsyncIOMotorCollection.insert_one 向集合中插入一个 单个 文档

以下示例向 inventory 集合插入一个新文档。如果文档没有指定 _id 字段,Motor 驱动程序会向新文档添加具有 ObjectId 值的 _id 字段。请参阅插入行为。

Collection.insertOne() 向集合中插入一个 单个 文档

以下示例将一个新的文档插入到 inventory 集合中。如果文档没有指定 _id 字段,Node.js 驱动程序会向新文档添加一个包含 ObjectId 值的 _id 字段。请参阅 插入行为。

MongoDB::Collection::insert_one() 插入一个 单个 文档 到集合中。

以下示例将一个新的文档插入到 inventory 集合中。如果文档没有指定 _id 字段,Perl 驱动程序会向新文档添加一个包含 ObjectId 值的 _id 字段。请参阅 插入行为。

MongoDB\\Collection::insertOne() 插入一个 单个 文档 到集合中。

以下示例将一个新的文档插入到 inventory 集合中。如果文档没有指定 _id 字段,PHP 驱动程序会向新文档添加一个包含 ObjectId 值的 _id 字段。请参阅 插入行为。

pymongo.collection.Collection.insert_one 插入一个 单个 文档 到集合中。

以下示例将一个新的文档插入到 inventory 集合中。如果文档没有指定 _id 字段,PyMongo 驱动程序会自动在新的文档中添加一个具有 ObjectId 值的 _id 字段。请参阅插入行为。

Mongo::Collection#insert_one() 将单个 文档 插入到集合中。

以下示例将一个新的文档插入到 inventory 集合中。如果文档没有指定 _id 字段,Ruby 驱动程序会在新的文档中添加一个具有 ObjectId 值的 _id 字段。请参阅插入行为。

collection.insertOne() 将单个 文档 插入到集合中。

以下示例将一个新的文档插入到 inventory 集合中。如果文档没有指定 _id 字段,Scala 驱动程序会在新的文档中添加一个具有 ObjectId 值的 _id 字段。请参阅插入行为。

db.inventory.insertOne(
{ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
)
Compass insert new document into collection
mongoc_collection_t *collection;
bson_t *doc;
bool r;
bson_error_t error;
collection = mongoc_database_get_collection (db, "inventory");
doc = BCON_NEW (
"item", BCON_UTF8 ("canvas"),
"qty", BCON_INT64 (100),
"tags", "[",
BCON_UTF8 ("cotton"),
"]",
"size", "{",
"h", BCON_DOUBLE (28),
"w", BCON_DOUBLE (35.5),
"uom", BCON_UTF8 ("cm"),
"}");
r = mongoc_collection_insert_one (collection, doc, NULL, NULL, &error);
bson_destroy (doc);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}
var document = new BsonDocument
{
{ "item", "canvas" },
{ "qty", 100 },
{ "tags", new BsonArray { "cotton" } },
{ "size", new BsonDocument { { "h", 28 }, { "w", 35.5 }, { "uom", "cm" } } }
};
collection.InsertOne(document);
result, err := coll.InsertOne(
context.TODO(),
bson.D{
{"item", "canvas"},
{"qty", 100},
{"tags", bson.A{"cotton"}},
{"size", bson.D{
{"h", 28},
{"w", 35.5},
{"uom", "cm"},
}},
})
Document canvas = new Document("item", "canvas")
.append("qty", 100)
.append("tags", singletonList("cotton"));
Document size = new Document("h", 28)
.append("w", 35.5)
.append("uom", "cm");
canvas.put("size", size);
Publisher<Success> insertOnePublisher = collection.insertOne(canvas);
Document canvas = new Document("item", "canvas")
.append("qty", 100)
.append("tags", singletonList("cotton"));
Document size = new Document("h", 28)
.append("w", 35.5)
.append("uom", "cm");
canvas.put("size", size);
collection.insertOne(canvas);
result = collection.insertOne(
Document("item", "canvas")
.append("qty", 100)
.append("tags", listOf("cotton"))
.append("size", Document("h", 28)
.append("w", 35.5)
.append("uom", "cm")
)
await db.inventory.insert_one(
{
"item": "canvas",
"qty": 100,
"tags": ["cotton"],
"size": {"h": 28, "w": 35.5, "uom": "cm"},
}
)
await db.collection('inventory').insertOne({
item: 'canvas',
qty: 100,
tags: ['cotton'],
size: { h: 28, w: 35.5, uom: 'cm' }
});
$db->coll("inventory")->insert_one(
{
item => "canvas",
qty => 100,
tags => ["cotton"],
size => { h => 28, w => 35.5, uom => "cm" }
}
);
$insertOneResult = $db->inventory->insertOne([
'item' => 'canvas',
'qty' => 100,
'tags' => ['cotton'],
'size' => ['h' => 28, 'w' => 35.5, 'uom' => 'cm'],
]);
db.inventory.insert_one(
{
"item": "canvas",
"qty": 100,
"tags": ["cotton"],
"size": {"h": 28, "w": 35.5, "uom": "cm"},
}
)
client[:inventory].insert_one({ item: 'canvas',
qty: 100,
tags: [ 'cotton' ],
size: { h: 28, w: 35.5, uom: 'cm' } })
collection.insertOne(
Document("item" -> "canvas", "qty" -> 100, "tags" -> Seq("cotton"), "size" -> Document("h" -> 28, "w" -> 35.5, "uom" -> "cm"))
).execute()

insertOne() 返回一个包含新插入文档的 _id 字段值的文档。有关返回文档的示例,请参阅db.collection.insertOne() 参考。

注意

MongoDB Compass 会自动生成 _id 字段及其值。生成的 ObjectId 由一个唯一的随机生成的十六进制值组成。

在插入文档之前,您可以更改此值,只要它保持唯一且是有效的 ObjectId。有关 _id 字段的更多信息,请参阅_id 字段。

mongoc_collection_insert_one 函数在成功时返回 true,如果存在无效参数或服务器或网络错误,则返回 false 并设置错误。

Collection.InsertOne 函数返回一个 InsertOneResult 实例,其 InsertedID 属性包含新插入文档的 _id

com.mongodb.reactivestreams.client.MongoCollection.insertOne 返回一个 Publisher 对象。当订阅者请求数据时,Publisher 将文档插入到集合中。

com.mongodb.client.MongoCollection.insertOne 返回一个 InsertOneResult 实例。您可以通过调用结果对象的 getInsertedId() 方法来访问插入文档的 _id 字段。

MongoCollection.insertOne 返回一个 InsertOneResult 实例。您可以通过访问结果的 insertedId 字段来访问插入文档的 _id 字段。

insert_one 返回一个 pymongo.results.InsertOneResult 实例,其中 inserted_id 字段包含新插入文档的 _id

insertOne() 返回一个提供 result 的 promise。该 result.insertedId promise 包含新插入文档的 _id

在成功插入后,insert_one() 方法返回一个 MongoDB::InsertOneResult 实例,其中 inserted_id 属性包含新插入文档的 _id

在成功插入后,insertOne() 方法返回一个 MongoDB\\InsertOneResult 实例,其 getInsertedId() 方法返回新插入文档的 _id

insert_one 返回一个 pymongo.results.InsertOneResult 的实例,其中 inserted_id 字段包含新插入文档的 _id

在成功插入后,insert_one() 方法返回一个 Mongo::Operation::Result 的实例,其中 inserted_id 属性包含新插入文档的 _id

在成功插入后,collection.insertOne() 方法返回一个包含新插入文档的 _idcollection.insertOne().results(); 实例。

要检索您刚刚插入的文档,请查询集合:

db.inventory.find( { item: "canvas" } )
Query for matching documents in a collection

在 MongoDB Compass 查询栏中指定一个过滤器,然后点击 查找 来执行查询。

上述过滤器指定 MongoDB Compass 只返回字段 item 等于 canvas 的文档。

有关 MongoDB Compass 查询栏的更多信息,请参阅 Compass 的 查询栏 文档。

mongoc_collection_t *collection;
bson_t *filter;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW ("item", BCON_UTF8 ("canvas"));
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("item", "canvas");
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{{"item", "canvas"}},
)
FindPublisher<Document> findPublisher = collection.find(eq("item", "canvas"));
FindIterable<Document> findIterable = collection.find(eq("item", "canvas"));
val flowInsertOne = collection
.find(eq("item", "canvas"))
.firstOrNull()
cursor = db.inventory.find({"item": "canvas"})
const cursor = db.collection('inventory').find({ item: 'canvas' });
$cursor = $db->coll("inventory")->find( { item => "canvas" } );
$cursor = $db->inventory->find(['item' => 'canvas']);
cursor = db.inventory.find({"item": "canvas"})
client[:inventory].find(item: 'canvas')
val observable = collection.find(equal("item", "canvas"))

db.collection.insertMany() 可以将多个 文档 插入到集合中。将文档数组传递给此方法。

以下示例将三个新文档插入到 inventory 集合中。如果文档没有指定 _id 字段,MongoDB 会为每个文档添加一个具有 ObjectId 值的 _id 字段。请参阅 插入行为。

mongoc_bulk_operation_insert_with_opts 可以将多个 文档 插入到集合中。您必须将文档的可迭代对象传递给此方法。

以下示例将三个新文档插入到 inventory 集合中。如果文档没有指定 _id 字段,C 驱动程序会为每个文档添加一个具有 ObjectId 值的 _id 字段。请参阅 插入行为。

IMongoCollection.InsertMany() 可以将多个 文档 插入到集合中。将文档的可枚举集合传递给此方法。

以下示例将三个新文档插入到 inventory 集合中。如果文档没有指定 _id 字段,驱动程序会为每个文档添加一个具有 ObjectId 值的 _id 字段。请参阅 插入行为。

Collection.InsertMany 可以向集合中插入 多个 文档

以下示例将三个新文档插入到 inventory 集合中。如果文档没有指定 _id 字段,驱动程序会为每个文档添加一个具有 ObjectId 值的 _id 字段。请参阅 插入行为。

com.mongodb.reactivestreams.client.MongoCollection.html.insertMany 使用 Java Reactive Streams Driver 插入以下文档

{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } }
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } }
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }

以下示例将三个新文档插入到 inventory 集合中。如果文档没有指定 _id 字段,驱动程序会为每个文档添加一个具有 ObjectId 值的 _id 字段。请参阅 插入行为。

com.mongodb.client.MongoCollection.insertMany 可以向集合中插入 多个 文档。将文档列表传递给此方法。

以下示例将三个新文档插入到 inventory 集合中。如果文档没有指定 _id 字段,驱动程序会为每个文档添加一个具有 ObjectId 值的 _id 字段。请参阅 插入行为。

MongoCollection.insertMany 向集合中插入 多个 文档。将文档列表作为参数传递给该方法。

以下示例向 inventory 集合中插入三个新文档。如果文档没有指定 _id 字段,驱动程序将为每个文档添加一个 ObjectId 值。参见 插入行为。

motor.motor_asyncio.AsyncIOMotorCollection.insert_many 可以向集合中插入 多个 文档。将文档的可迭代对象传递给此方法。

以下示例向 inventory 集合插入三个新文档。如果文档未指定 _id 字段,PyMongo 驱动程序将为每个文档添加一个包含 ObjectId 值的 _id 字段。请参阅 插入行为。

Collection.insertMany() 可以向集合中插入 多个 文档。将文档数组传递给此方法。

以下示例向 inventory 集合插入三个新文档。如果文档未指定 _id 字段,Node.js 驱动程序将为每个文档添加一个包含 ObjectId 值的 _id 字段。请参阅 插入行为。

MongoDB::Collection::insert_many() 可以将多个文档插入到集合中。将文档的数组引用传递给该方法。

以下示例将三个新文档插入到 inventory 集合中。如果文档没有指定 _id 字段,Perl 驱动程序将为每个文档添加带有 ObjectId 值的 _id 字段。请参阅 插入行为。

MongoDB\\Collection::insertMany() 可以将多个文档插入到集合中。将文档数组传递给该方法。

以下示例将三个新文档插入到 inventory 集合中。如果文档没有指定 _id 字段,PHP 驱动程序将为每个文档添加带有 ObjectId 值的 _id 字段。请参阅 插入行为。

pymongo.collection.Collection.insert_many 可以将多个文档插入到集合中。将文档的可迭代对象传递给该方法。

以下示例向 inventory 集合插入三个新文档。如果文档未指定 _id 字段,PyMongo 驱动程序将为每个文档添加一个包含 ObjectId 值的 _id 字段。请参阅 插入行为。

Mongo::Collection#insert_many() 可以将多个文档插入到集合中。将文档数组传递给此方法。

以下示例将三个新文档插入到 inventory 集合中。如果文档没有指定 _id 字段,Ruby 驱动程序将为每个文档添加包含 ObjectId 值的 _id 字段。参见 插入行为。

collection.insertMany() 可以将多个文档插入到集合中。

以下示例将三个新文档插入到 inventory 集合中。如果文档没有指定 _id 字段,Scala 驱动程序将为每个文档添加包含 ObjectId 值的 _id 字段。参见 插入行为。

db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
])
[
{ "item": "canvas", "qty": 100, "size": { "h": 28, "w": 35.5, "uom": "cm" }, "status": "A" },
{ "item": "journal", "qty": 25, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" },
{ "item": "mat", "qty": 85, "size": { "h": 27.9, "w": 35.5, "uom": "cm" }, "status": "A" },
{ "item": "mousepad", "qty": 25, "size": { "h": 19, "w": 22.85, "uom": "cm" }, "status": "P" },
{ "item": "notebook", "qty": 50, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "P" },
{ "item": "paper", "qty": 100, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "D" },
{ "item": "planner", "qty": 75, "size": { "h": 22.85, "w": 30, "uom": "cm" }, "status": "D" },
{ "item": "postcard", "qty": 45, "size": { "h": 10, "w": 15.25, "uom": "cm" }, "status": "A" },
{ "item": "sketchbook", "qty": 80, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" },
{ "item": "sketch pad", "qty": 95, "size": { "h": 22.85, "w": 30.5, "uom": "cm" }, "status": "A" }
]

有关使用MongoDB Compass插入文档的说明,请参阅 插入文档。

mongoc_collection_t *collection;
mongoc_bulk_operation_t *bulk;
bson_t *doc;
bool r;
bson_error_t error;
bson_t reply;
collection = mongoc_database_get_collection (db, "inventory");
bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
doc = BCON_NEW (
"item", BCON_UTF8 ("journal"),
"qty", BCON_INT64 (25),
"tags", "[",
BCON_UTF8 ("blank"), BCON_UTF8 ("red"),
"]",
"size", "{",
"h", BCON_DOUBLE (14),
"w", BCON_DOUBLE (21),
"uom", BCON_UTF8 ("cm"),
"}");
r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error);
bson_destroy (doc);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}
doc = BCON_NEW (
"item", BCON_UTF8 ("mat"),
"qty", BCON_INT64 (85),
"tags", "[",
BCON_UTF8 ("gray"),
"]",
"size", "{",
"h", BCON_DOUBLE (27.9),
"w", BCON_DOUBLE (35.5),
"uom", BCON_UTF8 ("cm"),
"}");
r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error);
bson_destroy (doc);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}
doc = BCON_NEW (
"item", BCON_UTF8 ("mousepad"),
"qty", BCON_INT64 (25),
"tags", "[",
BCON_UTF8 ("gel"), BCON_UTF8 ("blue"),
"]",
"size", "{",
"h", BCON_DOUBLE (19),
"w", BCON_DOUBLE (22.85),
"uom", BCON_UTF8 ("cm"),
"}");
r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error);
bson_destroy (doc);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}
/* "reply" is initialized on success or error */
r = (bool) mongoc_bulk_operation_execute (bulk, &reply, &error);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
}
var documents = new BsonDocument[]
{
new BsonDocument
{
{ "item", "journal" },
{ "qty", 25 },
{ "tags", new BsonArray { "blank", "red" } },
{ "size", new BsonDocument { { "h", 14 }, { "w", 21 }, { "uom", "cm"} } }
},
new BsonDocument
{
{ "item", "mat" },
{ "qty", 85 },
{ "tags", new BsonArray { "gray" } },
{ "size", new BsonDocument { { "h", 27.9 }, { "w", 35.5 }, { "uom", "cm"} } }
},
new BsonDocument
{
{ "item", "mousepad" },
{ "qty", 25 },
{ "tags", new BsonArray { "gel", "blue" } },
{ "size", new BsonDocument { { "h", 19 }, { "w", 22.85 }, { "uom", "cm"} } }
},
};
collection.InsertMany(documents);
result, err := coll.InsertMany(
context.TODO(),
[]interface{}{
bson.D{
{"item", "journal"},
{"qty", int32(25)},
{"tags", bson.A{"blank", "red"}},
{"size", bson.D{
{"h", 14},
{"w", 21},
{"uom", "cm"},
}},
},
bson.D{
{"item", "mat"},
{"qty", int32(25)},
{"tags", bson.A{"gray"}},
{"size", bson.D{
{"h", 27.9},
{"w", 35.5},
{"uom", "cm"},
}},
},
bson.D{
{"item", "mousepad"},
{"qty", 25},
{"tags", bson.A{"gel", "blue"}},
{"size", bson.D{
{"h", 19},
{"w", 22.85},
{"uom", "cm"},
}},
},
})
Document journal = new Document("item", "journal")
.append("qty", 25)
.append("tags", asList("blank", "red"));
Document journalSize = new Document("h", 14)
.append("w", 21)
.append("uom", "cm");
journal.put("size", journalSize);
Document mat = new Document("item", "mat")
.append("qty", 85)
.append("tags", singletonList("gray"));
Document matSize = new Document("h", 27.9)
.append("w", 35.5)
.append("uom", "cm");
mat.put("size", matSize);
Document mousePad = new Document("item", "mousePad")
.append("qty", 25)
.append("tags", asList("gel", "blue"));
Document mousePadSize = new Document("h", 19)
.append("w", 22.85)
.append("uom", "cm");
mousePad.put("size", mousePadSize);
Publisher<Success> insertManyPublisher = collection.insertMany(asList(journal, mat, mousePad));
Document journal = new Document("item", "journal")
.append("qty", 25)
.append("tags", asList("blank", "red"));
Document journalSize = new Document("h", 14)
.append("w", 21)
.append("uom", "cm");
journal.put("size", journalSize);
Document mat = new Document("item", "mat")
.append("qty", 85)
.append("tags", singletonList("gray"));
Document matSize = new Document("h", 27.9)
.append("w", 35.5)
.append("uom", "cm");
mat.put("size", matSize);
Document mousePad = new Document("item", "mousePad")
.append("qty", 25)
.append("tags", asList("gel", "blue"));
Document mousePadSize = new Document("h", 19)
.append("w", 22.85)
.append("uom", "cm");
mousePad.put("size", mousePadSize);
collection.insertMany(asList(journal, mat, mousePad));
results = collection.insertMany(
listOf(
Document("item", "journal")
.append("qty", 25)
.append("tags", listOf("blank", "red"))
.append("size", Document("h", 14)
.append("w", 21)
.append("uom", "cm")
),
Document("item", "mat")
.append("qty", 25)
.append("tags", listOf("gray"))
.append("size", Document("h", 27.9)
.append("w", 35.5)
.append("uom", "cm")
),
Document("item", "mousepad")
.append("qty", 25)
.append("tags", listOf("gel", "blue"))
.append("size", Document("h", 19)
.append("w", 22.85)
.append("uom", "cm")
)
)
await db.inventory.insert_many(
[
{
"item": "journal",
"qty": 25,
"tags": ["blank", "red"],
"size": {"h": 14, "w": 21, "uom": "cm"},
},
{
"item": "mat",
"qty": 85,
"tags": ["gray"],
"size": {"h": 27.9, "w": 35.5, "uom": "cm"},
},
{
"item": "mousepad",
"qty": 25,
"tags": ["gel", "blue"],
"size": {"h": 19, "w": 22.85, "uom": "cm"},
},
]
)
await db.collection('inventory').insertMany([
{
item: 'journal',
qty: 25,
tags: ['blank', 'red'],
size: { h: 14, w: 21, uom: 'cm' }
},
{
item: 'mat',
qty: 85,
tags: ['gray'],
size: { h: 27.9, w: 35.5, uom: 'cm' }
},
{
item: 'mousepad',
qty: 25,
tags: ['gel', 'blue'],
size: { h: 19, w: 22.85, uom: 'cm' }
}
]);
$db->coll("inventory")->insert_many(
[
{
item => "journal",
qty => 25,
tags => [ "blank", "red" ],
size => { h => 14, w => 21, uom => "cm" }
},
{
item => "mat",
qty => 85,
tags => ["gray"],
size => { h => 27.9, w => 35.5, uom => "cm" }
},
{
item => "mousepad",
qty => 25,
tags => [ "gel", "blue" ],
size => { h => 19, w => 22.85, uom => "cm" }
}
]
);
$insertManyResult = $db->inventory->insertMany([
[
'item' => 'journal',
'qty' => 25,
'tags' => ['blank', 'red'],
'size' => ['h' => 14, 'w' => 21, 'uom' => 'cm'],
],
[
'item' => 'mat',
'qty' => 85,
'tags' => ['gray'],
'size' => ['h' => 27.9, 'w' => 35.5, 'uom' => 'cm'],
],
[
'item' => 'mousepad',
'qty' => 25,
'tags' => ['gel', 'blue'],
'size' => ['h' => 19, 'w' => 22.85, 'uom' => 'cm'],
],
]);
db.inventory.insert_many(
[
{
"item": "journal",
"qty": 25,
"tags": ["blank", "red"],
"size": {"h": 14, "w": 21, "uom": "cm"},
},
{
"item": "mat",
"qty": 85,
"tags": ["gray"],
"size": {"h": 27.9, "w": 35.5, "uom": "cm"},
},
{
"item": "mousepad",
"qty": 25,
"tags": ["gel", "blue"],
"size": {"h": 19, "w": 22.85, "uom": "cm"},
},
]
)
client[:inventory].insert_many([{ item: 'journal',
qty: 25,
tags: ['blank', 'red'],
size: { h: 14, w: 21, uom: 'cm' }
},
{ item: 'mat',
qty: 85,
tags: ['gray'],
size: { h: 27.9, w: 35.5, uom: 'cm' }
},
{ item: 'mousepad',
qty: 25,
tags: ['gel', 'blue'],
size: { h: 19, w: 22.85, uom: 'cm' }
}
])
collection.insertMany(Seq(
Document("item" -> "journal", "qty" -> 25, "tags" -> Seq("blank", "red"), "size" -> Document("h" -> 14, "w" -> 21, "uom" -> "cm")),
Document("item" -> "mat", "qty" -> 85, "tags" -> Seq("gray"), "size" -> Document("h" -> 27.9, "w" -> 35.5, "uom" -> "cm")),
Document("item" -> "mousepad", "qty" -> 25, "tags" -> Seq("gel", "blue"), "size" -> Document("h" -> 19, "w" -> 22.85, "uom" -> "cm"))
)).execute()

insertMany() 返回一个包含新插入文档 _id 字段值的文档。参见 参考 以获取示例。

要检索插入的文档,请 查询集合:

mongoc_bulk_operation_insert_with_opts 函数在成功时返回 true,如果传入无效参数则返回 false

要检索插入的文档,请使用 mongoc_collection_find_with_opts 查询集合:查询文档:

要检索插入的文档,请 查询集合:

要检索插入的文档,请 查询集合:

com.mongodb.reactivestreams.client.MongoCollection.html.insertMany 返回一个 Publisher 对象。当订阅者请求数据时,Publisher 将文档插入到集合中。

要检索插入的文档,请 查询集合:

要检索插入的文档,请 查询集合:

MongoCollection.insertMany() 返回一个 InsertManyResult 实例。在 InsertManyResultinsertedIds 字段中包含插入文档的 _id 值。

要检索插入的文档,请 查询集合:

insert_many 返回一个实例,该实例的 inserted_ids 字段是一个包含每个新插入文档的 _id 的列表。

要检索插入的文档,请 查询集合:

insertMany() 返回一个 promise,该 promise 提供一个 result。其中,result.insertedIds 字段包含一个数组,该数组包含每个新插入文档的 _id

要检索插入的文档,请 查询集合:

在成功插入后,insert_many() 方法返回一个 MongoDB::InsertManyResult 实例,该实例的 inserted_ids 属性是一个包含每个新插入文档的 _id 的列表。

要检索插入的文档,请 查询集合:

在成功插入后,insertMany() 方法返回一个 MongoDB\\InsertManyResult 实例,该实例的 getInsertedIds() 方法返回每个新插入文档的 _id

要检索插入的文档,请 查询集合:

insert_many 返回一个 pymongo.results.InsertManyResult 实例,其中 inserted_ids 字段是一个包含每个新插入文档的 _id 的列表。

要检索插入的文档,请 查询集合:

在成功插入后,insert_many() 方法返回一个 Mongo::BulkWrite::Result 实例,其 inserted_ids 属性是一个包含每个新插入文档的 _id 的列表。

要检索插入的文档,请 查询集合:

在成功插入后,insertMany() 方法返回一个带有类型参数的 Observable 对象,该对象表示操作已完成或者抛出 com.mongodb.DuplicateKeyExceptioncom.mongodb.MongoException

要检索插入的文档,请 查询集合:

db.inventory.find( {} )
Compass select all documents in collection
mongoc_collection_t *collection;
bson_t *filter;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW (NULL);
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);

请务必调用以下适当的方法来清理任何打开的资源

var filter = Builders<BsonDocument>.Filter.Empty;
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{},
)
FindPublisher<Document> findPublisher = collection.find(new Document());
FindIterable<Document> findIterable = collection.find(new Document());
val flowInsertMany = collection
.find(empty())
cursor = db.inventory.find({})
const cursor = db.collection('inventory').find({});
$cursor = $db->coll("inventory")->find( {} );
$cursor = $db->inventory->find([]);
cursor = db.inventory.find({})
client[:inventory].find({})
var findObservable = collection.find(Document())

如果集合当前不存在,插入操作将创建集合。

在MongoDB中,每个存储在集合中的文档都需要一个唯一的 _id 字段,该字段作为 主键。如果插入的文档省略了 _id 字段,MongoDB 驱动程序将自动为 _id 字段生成一个 ObjectId

这同样适用于带有 upsert: true. 的更新操作插入的文档。

在MongoDB中,所有写操作在单个文档级别上都是原子的。有关MongoDB和原子性的更多信息,请参阅原子性和事务

使用写入关注,您可以指定MongoDB对写入操作请求的确认级别。有关详细信息,请参阅写入关注。

提示

另请参阅

返回

CRUD 操作