1. Overview
1.概述
MongoDB is the most popular open-source and distributed document-oriented NoSQL database. A document in MongoDB is a data structure with JSON-like objects having field and value pairs.
MongoDB是最流行的开源和分布式面向文档的NoSQL数据库。MongoDB中的文档是一个数据结构,其中有类似JSON的对象,具有字段和值对。
In order to insert documents into a MongoDB collection, we can use different methods such as insert(), insertOne() and insertMany().
为了向MongoDB集合插入文档,我们可以使用不同的方法,如insert(),insertOne()和insertMany()。
In this tutorial, we’ll discuss how to insert an array inside a MongoDB document. First, we’ll look at inserting an array into documents using the MongoDB Shell query. Then we’ll use the MongoDB Java driver code.
在本教程中,我们将讨论如何在MongoDB文档中插入一个数组。首先,我们将研究如何使用 MongoDB Shell 查询将数组插入文档中。然后我们将使用MongoDB Java 驱动程序代码。
2. Database Initialization
2.数据库初始化
Before we move on to the insert queries, let’s first create a database. Let’s call it baeldung. We’ll also create a sample collection named student:
在我们继续进行插入查询之前,让我们首先创建一个数据库。让我们把它叫做baeldung。我们还将创建一个名为student:的样本集合。
use baeldung;
db.createCollection(student);
With this command, our sample baeldung database and student collection are successfully set up. We will use these to demonstrate in all the examples.
通过这个命令,我们的样本baeldung数据库和student集合已经成功建立。我们将在所有的例子中使用这些来演示。
3. Using the MongoDB Shell
3.使用MongoDB Shell
To insert an array into a collection using the MongoDB Shell, we can simply pass the array as a JSON Array type to the shell:
要使用MongoDB Shell将一个数组插入到一个集合中,我们可以简单地将数组作为JSON数组类型传递给shell。
db.student.insert({
"studentId" : "STU1",
"name" : "Avin",
"Age" : 12,
"courses" : ["Science", "Math"]
});
The above query inserts a single document with an array in the student collection. We can verify the result by querying the documents of the student collection using the find operator:
上面的查询在student集合中插入了一个带有数组的单个文档。我们可以通过使用find操作符来查询student集合中的文档来验证结果。
db.student.find();
The above query returns the inserted student collection document:
上述查询返回插入的student集合文档。
{
"_id" : ObjectId("631da4197581ba6bc1d2524d"),
"studentId" : "STU1",
"name" : "Avin",
"Age" : 12,
"courses" : [ "Science", "Math" ]
}
4. Insert Operation Using Java Driver Code
4.使用Java驱动代码的插入操作
The MongoDB Java Driver provides various convenience methods to help us insert documents into a collection:
MongoDB的Java驱动提供了各种方便的方法来帮助我们将文档插入到一个集合中。
- insert() – Inserts a single document or multiple documents into a collection
- insertOne() – Inserts a single document into a collection
- insertMany() – Inserts multiple documents into a collection
Any of the above methods can be used to perform the insert operation on the MongoDB collection.
上述任何方法都可用于对MongoDB集合执行insert操作。
Next, let’s dive into implementing the array insertion operation using the Java MongoDB Driver. The MongoDB Java driver supports both the DBObject and BSON document.
接下来,让我们深入了解一下使用Java MongoDB驱动程序实现数组插入操作。MongoDB的Java驱动同时支持DBObject和BSON文档。
5. Using the DBObject
5.使用DBObject
Here, the DBObject is part of the MongoDB legacy driver, but it is deprecated in newer versions of MongoDB.
在这里,DBObject是MongoDB传统驱动的一部分,但是在MongoDB的新版本中,它已经被废弃。
Let’s insert a DBObject document with an array into the student collection:
让我们在DBObject文档中插入一个带有数组的student集合。
BasicDBList coursesList = new BasicDBList();
coursesList.add("Chemistry");
coursesList.add("Science");
DBObject student = new BasicDBObject().append("studentId", "STU1")
.append("name", "Jim")
.append("age", 13)
.append("courses", coursesList);
dbCollection.insert(student);
The above query inserts a single DBObject document with an array into the student collection.
上面的查询将一个带有数组的单个DBObject文档插入到student集合。
6. Using the BSON Document
6.使用BSON文件
The BSON Document is the new way to access the MongoDB document in Java and is built with the newer client stack. Luckily, it’s also easier to use.
BSON文档是在Java中访问MongoDB文档的新方式,并且是用较新的客户端栈构建的。幸运的是,它也更容易使用。
The Java driver provides an org.bson.Document class to insert a Bson document object with an array into the student collection.
Java驱动提供了一个org.bson.Document类,用于将一个带有数组的Bson文档对象插入到student集合。
6.1. Insert a Single Document with an Array
6.1.用一个数组插入一个单一文件
Firstly, let’s insert a single document with an array into the collection using the insertOne() method:
首先,让我们使用insertOne()方法在集合中插入一个带有数组的单个文档。
List coursesList = new ArrayList<>();
coursesList.add("Science");
coursesList.add("Geography");
Document student = new Document().append("studentId", "STU2")
.append("name", "Sam")
.append("age", 13)
.append("courses", coursesList);
collection.insertOne(student);
The above query inserts a single document with an array into the student collection. It is important to note that the append(String, Object) method of the Document class accepts an Object as the value. We can pass a List of any Object type as the value to insert it as an array into the document.
上面的查询将一个带有数组的单个文档插入到student集合中。需要注意的是,Document类的append(String, Object)方法接受一个Object作为值。我们可以传递任何Object类型的List作为值,将其作为一个数组插入文档中。
6.2. Insert Multiple Documents with an Array
6.2.用一个数组插入多个文件
Let’s insert multiple documents with an array into the collection using the insertMany() method:
让我们使用insertMany()方法将多个带数组的文档插入集合。
List coursesList1 = new ArrayList<>();
coursesList1.add("Chemistry");
coursesList1.add("Geography");
Document student1 = new Document().append("studentId", "STU3")
.append("name", "Sarah")
.append("age", 12)
.append("courses", coursesList1);
List coursesList2 = new ArrayList<>();
coursesList2.add("Math");
coursesList2.add("History");
Document student2 = new Document().append("studentId", "STU4")
.append("name", "Tom")
.append("age", 13)
.append("courses", coursesList2);
List<Document> students = new ArrayList<>();
students.add(student1);
students.add(student2);
collection.insertMany(students);
The above query inserts multiple documents with an array into the student collection.
上面的查询将多个带数组的文件插入到student集合。
6.3. Insert an Object Array
6.3.插入一个对象阵列
Finally, let’s insert an Object array type of document into the MongoDB collection:
最后,让我们在MongoDB集合中插入一个Object数组类型的文档。
Document course1 = new Document().append("name", "C1")
.append("points", 5);
Document course2 = new Document().append("name", "C2")
.append("points", 7);
List<Document> coursesList = new ArrayList<>();
coursesList.add(course1);
coursesList.add(course2);
Document student = new Document().append("studentId", "STU5")
.append("name", "Sam")
.append("age", 13)
.append("courses", coursesList);
collection.insertOne(student);
The above query inserts multiple documents with an Object array into the student collection. Here, we have inserted a document having a list of documents as an array into the collection. Similarly, we can construct any complex array Object and insert it into a MongoDB collection.
上面的查询将多个带有Object数组的文档插入到student集合。在这里,我们已经将一个具有数组形式的文档列表插入到集合中。同样,我们可以构建任何复杂的数组Object并将其插入到MongoDB集合中。
7. Conclusion
7.结语
In this article, we’ve seen various ways to insert documents with an array Object into the MongoDB collection. We discussed these use cases using both the MongoDB Shell query as well as the corresponding Java driver code implementation.
在本文中,我们看到了将带有数组Object的文档插入MongoDB集合的各种方法。我们使用MongoDB Shell查询以及相应的Java驱动代码实现来讨论这些用例。
With the Java drive code, we first looked at the implementation using the deprecated DBObject class. Then, we learned to implement the same using the new BSON Document class.
通过Java驱动代码,我们首先看了使用已废弃的DBObject类的实现。然后,我们学会了使用新的BSON Document类来实现。
The implementations of all these examples and code snippets are available over on GitHub.
所有这些例子和代码片段的实现都可以在GitHub上找到。