我试图在一个事务中插入一个带有序列号的文档,来自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驱动程序。
答案 0 :(得分:5)
遵循creating an Auto-Incrementing Sequence Field的文档,我们将其用于Java 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
)的对象。
那是因为,根据documentation:
默认情况下,返回的文档不包含对更新所做的修改。要返回包含更新修改的文档,请使用新选项。
我们需要返回包含更新修改的文档。
答案 1 :(得分:2)
您不能在一个事务中执行此操作,甚至您的javascript示例在执行插入之前执行findAndModify。
如果您希望将文档插入到该集合中的文档数量不断增加的集合中,您可以使用count方法并添加1.
final DBCollection col = db.getCollection("myCollection");
col.insert(new BasicDBObject("number", col.count() + 1));
或者使用单独的“counter”集合和findAndModify()代替count()
答案 2 :(得分:1)
试试这个:
DBCollection collection = database.getCollection("testCollection");
// create an increment query
DBObject modifier = new BasicDBObject("counter", 1);
DBObject incQuery = new BasicDBObject("$inc", modifier);
// create a search query
DBObject searchQuery = new BasicDBObject("name", "someName");
// increment a counter value atomically
DBObject res = collection.findAndModify(searchQuery, incQuery);
答案 3 :(得分:1)
正如亚历克斯所写,但此代码适用于3.x版本
import com.mongodb.*;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
public class TestAutoIncrement {
private final static String DB_NAME = "MyTestDB";
private final static String TEST_COLLECTION = "testCollection";
private final static String COUNTERS_COLLECTION = "countersCollection";
private static MongoCollection<Document> testCollection;
private static MongoCollection<Document> countersCollection;
public static void main(String[] args) {
MongoClient mongoClient = new MongoClient();
MongoDatabase database = mongoClient.getDatabase(DB_NAME);
testCollection = database.getCollection(TEST_COLLECTION);
countersCollection = database.getCollection(COUNTERS_COLLECTION);
if (countersCollection.count() == 0) {
createCountersCollection();
}
createTestCollection();
mongoClient.close();
}
public static void createCountersCollection() {
Document document = new Document();
document.append("_id", "userid");
document.append("seq", 1);
countersCollection.insertOne(document);
}
public static Object getNextSequence(String name) {
Document searchQuery = new Document("_id", name);
Document increase = new Document("seq", 1);
Document updateQuery = new Document("$inc", increase);
Document result = countersCollection.findOneAndUpdate(searchQuery, updateQuery);
return result.get("seq");
}
public static void createTestCollection() {
Document document = new Document();
document.append("_id", getNextSequence("userid"));
document.append("name", "Dinah");
testCollection.insertOne(document);
document = new Document();
document.append("_id", getNextSequence("userid"));
document.append("name", "Jonny");
testCollection.insertOne(document);
document = new Document();
document.append("_id", getNextSequence("userid"));
document.append("name", "Brody");
testCollection.insertOne(document);
}
}