This page provides examples in:本页提供了以下示例:
This page provides examples of insert operations in MongoDB.本页提供了MongoDB中插入操作的示例。
Creating a Collection创建集合
If the collection does not currently exist, insert operations will create the collection.如果集合当前不存在,则插入操作将创建集合。
db.collection.insertOne()
inserts a single document into a collection.db.collection.insertOne()
将单个文档插入到集合中。
The following example inserts a new document into the 以下示例将新文档插入到inventory
collection. inventory
集合中。If the document does not specify an 如果文档未指定_id
field, MongoDB adds the _id
field with an ObjectId value to the new document. _id
字段,MongoDB将向新文档添加带有ObjectId值的_id
字段。See Insert Behavior.请参见插入行为。
To insert a single document using MongoDB Compass:要使用MongoDB Compass插入单个文档,请执行以下操作:
Object
types, add nested fields by clicking the last field’s number and selecting Add Field After …object
类型,通过单击最后一个字段的编号并选择“在...后面添加字段”来添加嵌套字段Array
types, add additional elements to the array by clicking the last element’s line number and selecting Add Array Element After …Array
类型,通过单击最后一个元素的行号并选择“在...后面添加数据元素”,将其他元素添加到数组中The following example inserts a new document into the 以下示例将新文档插入到test.inventory
collection:test.inventory
集合中:
pymongo.collection.Collection.insert_one()
inserts a single document into a collection.
The following example inserts a new document into the 以下示例将新文档插入到inventory
collection. inventory
集合中。If the document does not specify an 如果文档未指定_id
field, the PyMongo driver adds the _id
field with an ObjectId value to the new document. _id
字段,PyMongo驱动程序会将带有ObjectId值的_id
字段添加到新文档中。See Insert Behavior.请参见插入行为。
com.mongodb.client.MongoCollection.insertOne inserts a single document into a collection.com.mongodb.client.MongoCollection.insertOne将单个文档插入到集合中。
The following example inserts a new document into the 以下示例将新文档插入到inventory
collection. inventory
集合中。If the document does not specify an 如果文档未指定_id
field, the driver adds the _id
field with an ObjectId value to the new document. _id
字段,则驱动程序会将带有ObjectId值的_id
字段添加到新文档中。See Insert Behavior.请参见插入行为。
Collection.insertOne() inserts a single document into a collection.Collection.insertOne()将单个文档插入到集合中。
The following example inserts a new document into the 以下示例将新文档插入到inventory
collection. inventory
集合中。If the document does not specify an 如果文档未指定_id
field, the Node.js driver adds the _id
field with an ObjectId value to the new document. _id
字段,Node.js驱动程序会将带有ObjectId值的_id
字段添加到新文档中。See Insert Behavior.请参见插入行为。
MongoDB\Collection::insertOne()
inserts a single document into a collection.
The following example inserts a new document into the inventory
collection. If the document does not specify an _id
field, the PHP driver adds the _id
field with an ObjectId value to the new document. See Insert Behavior.请参见插入行为。
motor.motor_asyncio.AsyncIOMotorCollection.insert_one()
inserts a single document into a collection.
The following example inserts a new document into the inventory
collection. If the document does not specify an _id
field, the Motor driver adds the _id
field with an ObjectId value to the new document. See Insert Behavior.请参见插入行为。
com.mongodb.reactivestreams.client.MongoCollection.insertOne inserts a single document into a collection with the Java Reactive Streams Driver:
The following example inserts the document above into the inventory
collection. If the document does not specify an _id
field, the driver adds the _id
field with an ObjectId value to the new document. See Insert Behavior.请参见插入行为。
IMongoCollection.InsertOne() inserts a single document into a collection.
The following example inserts a new document into the inventory
collection. If the document does not specify an _id
field, the C# driver adds the _id
field with an ObjectId value to the new document. See Insert Behavior.请参见插入行为。
MongoDB::Collection::insert_one() inserts a single document into a collection.
The following example inserts a new document into the inventory
collection. If the document does not specify an _id
field, the Perl driver adds the _id
field with an ObjectId value to the new document. See Insert Behavior.请参见插入行为。
Mongo::Collection#insert_one() inserts a single document into a collection.
The following example inserts a new document into the inventory
collection. If the document does not specify an _id
field, the Ruby driver adds the _id
field with an ObjectId value to the new document. See Insert Behavior.请参见插入行为。
collection.insertOne() inserts a single document into a collection.
The following example inserts a new document into the inventory
collection. If the document does not specify an _id
field, the Scala driver adds the _id
field with an ObjectId value to the new document. See Insert Behavior.请参见插入行为。
Collection.InsertOne inserts a single document into a collection.
The following example inserts a new document into the inventory
collection. If the document does not specify an _id
field, the driver adds the _id
field with an ObjectId value to the new document. See Insert Behavior.请参见插入行为。
You can run the operation in the web shell below:您可以在下面的web shell中运行该操作:
insertOne()
returns a document that includes the newly inserted document’s _id
field value. insertOne()
返回包含新插入文档的_id
字段值的文档。For an example of a return document, see db.collection.insertOne() reference.有关返回文档的示例,请参阅db.collection.insertOne()
参考。
Note
MongoDB Compass generates the MongoDB Compass自动生成_id
field and its value automatically. _id
字段及其值。The generated ObjectId consists of a unique randomly generated hexadecimal value.生成的ObjectId由唯一的随机生成的十六进制值组成。
You can change this value prior to inserting your document so long as it remains unique and is a valid 您可以在插入文档之前更改此值,只要它保持唯一且是有效的ObjectId
. ObjectId
。For more information on the 有关_id
field, see _id Field._id
字段的详细信息,请参阅_id字段。
insert_one()
returns an instance of pymongo.results.InsertOneResult
whose inserted_id
field contains the _id
of the newly inserted document.
insertOne() returns a promise that provides a insertOne()返回提供结果的承诺。result
. The result.insertedId
promise contains the _id
of the newly inserted document.result.insertedId
承诺包含新插入文档的_id
。
Upon successful insert, the insertOne()
method returns an instance of MongoDB\InsertOneResult
whose getInsertedId()
method returns the _id
of the newly inserted document.
insert_one()
returns an instance of pymongo.results.InsertOneResult
whose inserted_id
field contains the _id
of the newly inserted document.
com.mongodb.reactivestreams.client.MongoCollection.insertOne returns a Publisher object. The Publisher
inserts the document into a collection when subscribers request data.
Upon successful insert, the insert_one() method returns an instance of MongoDB::InsertOneResult whose inserted_id
attribute contains the _id
of the newly inserted document.
Upon successful insert, the insert_one() method returns an instance of Mongo::Operation::Result, whose inserted_id
attribute contains the _id
of the newly inserted document.
Upon successful insert, the collection.insertOne() method returns an instance of collection.insertOne().results(); whose inserted_id
attribute contains the _id
of the newly inserted document.
Collection.InsertOne function returns an instance of InsertOneResult whose Collection.InsertOne函数返回InsertOneResult的实例,其InsertedID
attribute contains the _id
of the newly inserted document.InsertedID
属性包含新插入文档的_id
。
To retrieve the document that you just inserted, query the collection:要检索刚插入的文档,请查询集合:
Specify a filter in the MongoDB Compass query bar and click Find to execute the query.在MongoDB Compass查询栏中指定一个过滤器,然后单击“Find”执行查询。
The above filter specifies that MongoDB Compass only return documents where the 上面的过滤器指定MongoDB Compass仅返回item
field is equal to canvas
.item
字段等于画布的文档。
For more information on the MongoDB Compass Query Bar, see the Compass Query Bar documentation.有关MongoDBCompass查询栏的更多信息,请参阅Compass查询栏文档。
New in version 3.2.版本3.2中的新功能。
db.collection.insertMany()
can insert multiple documents into a collection. 可以将多个文档插入到集合中。Pass an array of documents to the method.将文档数组传递给方法。
The following example inserts three new documents into the 以下示例将三个新文档插入到inventory
collection. inventory
集合中。If the documents do not specify an 如果文档没有指定_id
field, MongoDB adds the _id
field with an ObjectId value to each document. _id
字段,MongoDB会向每个文档添加带有ObjectId值的_id
字段。See Insert Behavior.请参见插入行为。
Currently, MongoDB Compass does not support inserting multiple documents in a single operation.目前,MongoDB Compass不支持在一次操作中插入多个文档。
New in version 3.2.版本3.2中的新功能。
pymongo.collection.Collection.insert_many()
can insert multiple documents into a collection. 可以将多个文档插入到集合中。Pass an iterable of documents to the method.将一组文档传递给方法。
The following example inserts three new documents into the 以下示例将三个新文档插入到inventory
collection. inventory
集合中。If the documents do not specify an 如果文档未指定_id
field, the PyMongo driver adds the _id
field with an ObjectId value to each document. _id
字段,PyMongo驱动程序将向每个文档添加带有ObjectId值的_id
字段。See Insert Behavior.请参见插入行为。
New in version 3.2.版本3.2中的新功能。
com.mongodb.client.MongoCollection.insertMany can insert multiple documents into a collection. 可以将多个文档插入到集合中。Pass a list of documents to the method.将文档列表传递给方法。
The following example inserts three new documents into the 以下示例将三个新文档插入到inventory
collection. inventory
集合中。If the documents do not specify an 如果文档未指定_id
field, the driver adds the _id
field with an ObjectId value to each document. _id
字段,则驱动程序会将带有ObjectId值的_id
字段添加到每个文档中。See Insert Behavior.请参见插入行为。
New in version 3.2.版本3.2中的新功能。
Collection.insertMany() can insert multiple documents into a collection. 可以将多个文档插入到集合中。Pass an array of documents to the method.将文档数组传递给方法。
The following example inserts three new documents into the 以下示例将三个新文档插入到inventory
collection. inventory
集合中。If the documents do not specify an 如果文档没有指定_id
field, the Node.js driver adds the _id
field with an ObjectId value to each document. _id
字段,Node.js驱动程序会将带有ObjectId值的_id
字段添加到每个文档中。See Insert Behavior.请参见插入行为。
New in version 3.2.版本3.2中的新功能。
MongoDB\Collection::insertMany()
can insert multiple documents into a collection. Pass an array of documents to the method.
The following example inserts three new documents into the inventory
collection. If the documents do not specify an _id
field, the PHP driver adds the _id
field with an ObjectId value to each document. See Insert Behavior.请参见插入行为。
motor.motor_asyncio.AsyncIOMotorCollection.insert_many()
can insert multiple documents into a collection. Pass an iterable of documents to the method.
The following example inserts three new documents into the inventory
collection. If the documents do not specify an _id
field, the PyMongo driver adds the _id
field with an ObjectId value to each document. See Insert Behavior.请参见插入行为。
New in version 3.2.版本3.2中的新功能。
com.mongodb.reactivestreams.client.MongoCollection.html.insertMany inserts the following documents with the Java Reactive Streams Driver:
The following example inserts three new documents into the inventory
collection. If the documents do not specify an _id
field, the driver adds the _id
field with an ObjectId value to each document. See Insert Behavior.请参见插入行为。
New in version 3.2.版本3.2中的新功能。
IMongoCollection.InsertMany() can insert multiple documents into a collection. Pass an enumerable collection of documents to the method.
The following example inserts three new documents into the inventory
collection. If the documents do not specify an _id
field, the driver adds the _id
field with an ObjectId value to each document. See Insert Behavior.请参见插入行为。
New in version 3.2.版本3.2中的新功能。
MongoDB::Collection::insert_many() can insert multiple documents into a collection. Pass an array reference of documents to the method.
The following example inserts three new documents into the inventory
collection. If the documents do not specify an _id
field, the Perl driver adds the _id
field with an ObjectId value to each document. See Insert Behavior.请参见插入行为。
New in version 3.2.版本3.2中的新功能。
Mongo::Collection#insert_many() can insert multiple documents into a collection. Pass an array of documents to the method.
The following example inserts three new documents into the inventory
collection. If the documents do not specify an _id
field, the Ruby driver adds the _id
field with an ObjectId value to each document. See Insert Behavior.请参见插入行为。
New in version 3.2.版本3.2中的新功能。
collection.insertMany() can insert multiple documents into a collection.
The following example inserts three new documents into the inventory
collection. If the documents do not specify an _id
field, the Scala driver adds the _id
field with an ObjectId value to each document. See Insert Behavior.请参见插入行为。
Collection.InsertMany can insert multiple documents into a collection.
The following example inserts three new documents into the inventory
collection. If the documents do not specify an _id
field, the driver adds the _id
field with an ObjectId value to each document. See Insert Behavior.请参见插入行为。
You can run the operation in the web shell below:您可以在下面的web shell中运行该操作:
insertMany()
returns a document that includes the newly inserted documents 返回包含新插入的文档_id
field values. _id
字段值的文档。See the reference for an example.有关示例,请参阅参考资料。
To retrieve the inserted documents, query the collection:要检索已插入的文档,请查询集合:
insert_many()
returns an instance of pymongo.results.InsertManyResult
whose inserted_ids
field is a list containing the _id
of each newly inserted document.
To retrieve the inserted documents, query the collection:
To retrieve the inserted documents, query the collection:
insertMany() returns a promise that provides a 返回提供result
. result
的承诺。The result.insertedIds
field contains an array with the _id
of each newly inserted document.result.insertedIds
字段包含一个数组,其中包含每个新插入文档的_id
。
To retrieve the inserted documents, query the collection:要检索已插入的文档,请查询集合:
Upon successful insert, the insertMany()
method returns an instance of MongoDB\InsertManyResult
whose getInsertedIds()
method returns the _id
of each newly inserted document.
To retrieve the inserted documents, query the collection:
insert_many()
returns an instance of pymongo.results.InsertManyResult
whose inserted_ids
field is a list containing the _id
of each newly inserted document.
To retrieve the inserted documents, query the collection:
com.mongodb.reactivestreams.client.MongoCollection.html.insertMany returns a Publisher object. The Publisher
inserts the document into a collection when subscribers request data.
To retrieve the inserted documents, query the collection:
To retrieve the inserted documents, query the collection:
Upon successful insert, the insert_many() method returns an instance of MongoDB::InsertManyResult whose inserted_ids
attribute is a list containing the _id
of each newly inserted document.
To retrieve the inserted documents, query the collection:
Upon successful insert, the insert_many() method returns an instance of Mongo::BulkWrite::Result whose inserted_ids
attribute is a list containing the _id
of each newly inserted document.
To retrieve the inserted documents, query the collection:
Upon successful insert, the insertMany() method returns an Observable with a type parameter indicating when the operation has completed or with either a com.mongodb.DuplicateKeyException
or com.mongodb.MongoException
.
To retrieve the inserted documents, query the collection:
To retrieve the inserted documents, query the collection:
If the collection does not currently exist, insert operations will create the collection.如果集合当前不存在,则插入操作将创建集合。
_id
In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. 在MongoDB中,存储在集合中的每个文档都需要一个唯一的_id字段作为主键。If an inserted document omits the 如果插入的文档省略了_id
field, the MongoDB driver automatically generates an ObjectId for the _id
field._id
字段,MongoDB驱动程序会自动为_id
字段生成ObjectId。
This also applies to documents inserted through update operations with upsert: true.这也适用于通过upsert:true
的更新操作插入的文档。
All write operations in MongoDB are atomic on the level of a single document. MongoDB中的所有写操作都是单个文档级别的原子操作。For more information on MongoDB and atomicity, see Atomicity and Transactions有关MongoDB和原子性的更多信息,请参阅原子性和事务
With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. 对于写操作,您可以指定MongoDB为写操作请求的确认级别。For details, see Write Concern.有关详细信息,请参阅写关注事项。
See also参阅
motor.motor_asyncio.AsyncIOMotorCollection.insert_one()
motor.motor_asyncio.AsyncIOMotorCollection.insert_many()