Java的MongoDB:插入文档

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集合中创建一个具有namedescription字段的文档,我们将使用以下代码片段:

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值