We will continue with our collection User for data insertion which we created after connecting with MongoDB database. Let us consider various scenarios for inserting data in our collection User.
我们将继续连接用户MongoDB数据库之后创建的用于数据插入的集合User 。 让我们考虑在集合User中插入数据的各种方案。
使用默认的_id
将文档插入集合中 (Inserting Document into Collection with default _id
)
Suppose, we want to create a document in our User collection with fields name and description, we will make use of following code snippet:
假设我们要在User集合中创建一个具有name和description字段的文档,我们将使用以下代码片段:
package com.mongo;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
public class MakeConnection {
public static void main(String[] args) {
try {
// code to create the connection
MongoClient mongoClient = new MongoClient("localhost", 27017);
// code to connect to the database
DB db = mongoClient.getDB("DemoDB");
// get the User collection from DB
DBCollection userCollection = db.getCollection("User");
BasicDBObject bO = new BasicDBObject();
bO.put("name", "XYZ");
bO.put("description", "Data insertion in MongoDB");
// inserting data into collection
userCollection.insert(bO);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
In, the above code snippet, we used getCollection()
method to get the existing collection User. Then an object of BasicDBObject
is created and fields name and description are put in the object with values. The output generated can be seen using MongoDB Compass tool.
在上面的代码片段中,我们使用了getCollection()
方法来获取现有的集合User 。 然后,创建BasicDBObject
对象,并将字段名称和描述与值一起放入对象。 使用MongoDB指南针工具可以看到生成的输出。
![Data Insertion in MongoDB](https://i-blog.csdnimg.cn/blog_migrate/bfce3e70dbd5d759c64168babadc7a26.png)
As a result of executing the above code a new entry with name and description is created with a randomly generated _id
value.
执行上述代码的结果是,使用随机生成的_id
值创建了具有名称和描述的新条目。
使用自定义_id
将文档插入集合中 (Inserting Document into Collection with custom _id
)
While creating document(inserting data) in any collection, mongoDB provides its own unique id referred as _id. If we want to provide our own custom id, we can provide it. Refer the below code snippet for the same.
在任何集合中创建文档(插入数据)时,mongoDB提供其自己的唯一ID,称为_id 。 如果我们要提供自己的自定义ID,则可以提供。 请参考下面的代码片段。
package com.mongo;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
public class MakeConnection {
public static void main(String[] args) {
try {
// code to create the connection
MongoClient mongoClient = new MongoClient("localhost", 27017);
// code to connect to the database
DB db = mongoClient.getDB("DemoDB");
// get the User collection from DB
DBCollection userCollection = db.getCollection("User");
BasicDBObject bO = new BasicDBObject();
// adding custom _id
bO.put("_id", "xyz123");
bO.put("name", "XYZ");
bO.put("description", "Data insertion in MongoDB");
// inserting data into collection
userCollection.insert(bO);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
When the above program is executed, a new document is inserted in the collection with the values provided along with the provided value for _id
field.
当执行上述程序时,新文档将与提供的值以及为_id
字段提供的值一起插入集合中。
![Data Insertion in MongoDB](https://i-blog.csdnimg.cn/blog_migrate/3b697994026342970eb9f95338cd6260.png)
In the output, we can find that document is created with our unique custom id: xyz123
.
在输出中,我们可以找到使用唯一的自定义ID xyz123
创建的文档。
翻译自: https://www.studytonight.com/mongodb/data-insertion-mongodb