使用来自java的自动增量字段在mongodb中插入文档 [英] Insert document in mongodb with autoincrement field from java

查看:38
本文介绍了使用来自java的自动增量字段在mongodb中插入文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个事务中从 java 插入一个带有序列号的文档.

I am trying to insert a document with a sequence number, in one transaction, from java.

类似的东西:

function getNextSequence(name) {
    var ret = db.counters.findAndModify(
        {
            query: { _id: name },
            update: { $inc: { seq: 1 } },
            new: true
        }
    );

    return ret.seq;
}
collection.insert({
    number: getNextSequence("userid"),
    name: "Some Name"
});

是否可以从 Java 执行此操作?最好有官方的java驱动.

Is it possible to do this from java? Preferably with the official java driver.

推荐答案

遵循 创建自动递增序列字段,我们将其调整为在 Java 中使用 Java MongoDB 驱动程序.

Following the documentation for creating an Auto-Incrementing Sequence Field, we adapt it to be used in Java with the Java MongoDB driver.

示例实现:

import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class TestAutoIncrement {

private final static String DB_NAME = "MyTestDB";
private final static String TEST_COLLECTION = "testCollection";
private final static String COUNTERS_COLLECTION = "countersCollection";

public static DBCollection testCollection;
public static DBCollection countersCollection;

public static void main(String[] args) {

    try {
        MongoClient mongoClient = new MongoClient();
        DB database = mongoClient.getDB(DB_NAME);
        testCollection = database.getCollection(TEST_COLLECTION);
        countersCollection = database.getCollection(COUNTERS_COLLECTION);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

    if (countersCollection.count() == 0) {
        createCountersCollection();
    }

    createTestCollection();
}

public static void createCountersCollection() {

    BasicDBObject document = new BasicDBObject();
    document.append("_id", "userid");
    document.append("seq", 0);
    countersCollection.insert(document);
}

public static Object getNextSequence(String name) {

    BasicDBObject searchQuery = new BasicDBObject("_id", name);
    BasicDBObject increase = new BasicDBObject("seq", 1);
    BasicDBObject updateQuery = new BasicDBObject("$inc", increase);
    DBObject result = countersCollection.findAndModify(searchQuery, null, null,
            false, updateQuery, true, false);

    return result.get("seq");
}

public static void createTestCollection() {

    BasicDBObject document = new BasicDBObject();
    document.append("_id", getNextSequence("userid"));
    document.append("name", "Sarah");
    testCollection.insert(document);

    document = new BasicDBObject();
    document.append("_id", getNextSequence("userid"));
    document.append("name", "Bob");
    testCollection.insert(document);

    document = new BasicDBObject();
    document.append("_id", getNextSequence("userid"));
    document.append("name", "Alex");
    testCollection.insert(document);
  }

}

必须特别注意findAndModify 方法.在 Java MongoDB 驱动程序 (2.12.4) 中,该方法具有 4 种不同的签名.
您必须使用一个允许您传递 query 对象、update 对象和 returnNew 布尔值(必须设置为 true).

Special attention must be paid to the findAndModify method. In the Java MongoDB driver (2.12.4), the method is available with 4 different signatures.
You must use one which lets you pass a query object, update object and returnNew boolean (which has to be set to true).

那是因为,根据文档:
默认情况下,返回的文档不包括对更新所做的修改.要返回对更新进行修改的文档,请使用新选项.

That's because, according to documentation:
By default, the returned document does not include the modifications made on the update. To return the document with the modifications made on the update, use the new option.

我们需要返回包含对更新所做修改的文档.

We need to return the document with the modifications made on the update.

这篇关于使用来自java的自动增量字段在mongodb中插入文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆