Insert Documents插入文档
On this page本页内容
The MongoDB shell provides the following methods to insert documents into a collection:MongoDB shell提供了以下方法将文档插入集合:
To insert a single document, use要插入单个文档,请使用db.collection.insertOne()
.db.collection.insertOne()
。To insert multiple documents, use要插入多个文档,请使用db.collection.insertMany()
.db.collection.insertMany()
。
The examples on this page reference the Atlas sample dataset. 本页的示例引用了Atlas示例数据集。You can create a free Atlas cluster and populate that cluster with sample data to follow along with these examples. 您可以创建一个免费的Atlas集群,并使用示例数据填充该集群,以配合这些示例。To learn more, see Get Started with Atlas.要了解更多信息,请参阅Atlas入门。
Insert a Single Document插入单个文档
db.collection.insertOne()
inserts a single document into a collection. db.collection.insertOne()
将单个文档插入到集合中。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 new document into the 要将新文档插入sample_mflix.movies
collection:sample_mflix.movies
集合,请执行以下操作:
use sample_mflix
db.movies.insertOne(
{
title: "The Favourite",
genres: [ "Drama", "History" ],
runtime: 121,
rated: "R",
year: 2018,
directors: [ "Yorgos Lanthimos" ],
cast: [ "Olivia Colman", "Emma Stone", "Rachel Weisz" ],
type: "movie"
}
)
insertOne()
returns a document that includes the newly inserted document's 返回一个文档,该文档包括新插入的文档的_id
field value._id
字段值。
To retrieve the inserted document, read the collection:要检索插入的文档,请参阅读取集合:
db.movies.find( { title: "The Favourite" } )
To ensure you return the document you inserted, you can instead query by 为了确保返回插入的文档,您可以改为按_id
._id
进行查询。
Insert Multiple Documents插入多个文档
db.collection.insertMany()
can insert multiple documents into a collection. db.collection.insertMany()
可以将多个文档插入到一个集合中。Pass an array of documents to the method. 将文档数组传递给该方法。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.请参见插入行为。
To insert two new documents into the 要将两个新文档插入sample_mflix.movies
collection:sample_mflix.movies
集合,请执行以下操作:
use sample_mflix
db.movies.insertMany([
{
title: "Jurassic World: Fallen Kingdom",
genres: [ "Action", "Sci-Fi" ],
runtime: 130,
rated: "PG-13",
year: 2018,
directors: [ "J. A. Bayona" ],
cast: [ "Chris Pratt", "Bryce Dallas Howard", "Rafe Spall" ],
type: "movie"
},
{
title: "Tag",
genres: [ "Comedy", "Action" ],
runtime: 105,
rated: "R",
year: 2018,
directors: [ "Jeff Tomsic" ],
cast: [ "Annabelle Wallis", "Jeremy Renner", "Jon Hamm" ],
type: "movie"
}
])
insertMany()
returns a document that includes the newly inserted documents' _id
field values.insertMany()
返回一个文档,其中包含新插入的文档的_id
字段值。
To read documents in the collection:要阅读集合中的文档,请执行以下操作:
db.movies.find( {} )
Insert Behavior插入行为
To learn more about the specific behavior of inserting documents, see Insert Behavior.若要了解有关插入文档的特定行为的详细信息,请参阅插入行为。
Learn More了解更多信息
To see more examples of inserting documents into a collection, see the要查看将文档插入集合的更多示例,请参阅insertOne()
anddb.collection.insertMany()
method pages.insertOne()
和db.collection.insertMany()
方法页面。To see all available methods to insert documents into a collection, see Additional Methods for Inserts若要查看将文档插入集合的所有可用方法,请参阅其他插入方法