MongoDB:如果不存在则插入记录,如果存在则忽略

umb*_*ile 7 java upsert mongodb

我有一系列MongoDB记录,如:

{"name":"Bob", "gpa":4} {"name":"Sarah", "gpa":3}

我会遇到可能会或可能不会属于同一个人的各种其他记录.如果他们是新人,我想接受他们,如果他们是我们以前见过的人,就不要更新他们.所以,如果我得到{"name":"Jim", "gpa":2},我想接受原样.如果我得到{"name":"Sarah", "gpa":4}(不同的GPA值),我想忽略它.这似乎不是将update"upsert"设置为yes 或者使用"upsert" 的逻辑findAndModify.

/sf/answers/228220331/建议一种方式(第一个字段上的唯一索引,插入记录,立即更新第二个字段)但它已经有几年了,我想知道是否有更好的方法现在只需一步即可完成此操作.

编辑:

接受的答案看起来很棒,但它对我不起作用!首先,我创建了索引(这是使用Java驱动程序):

nameIndex.put("name", 1);
nameIndex.put("unique", true);
queue.ensureIndex(nameIndex);
Run Code Online (Sandbox Code Playgroud)

我可以从命令行看到索引存在且是唯一的.然后,插入一个项目:

DBObject person = new BasicDBObject();
person.put("name", "Bob");
person.put("score", 200000);
queue.insert(person);
Run Code Online (Sandbox Code Playgroud)

之后,得分最高的项目的分数降至零:

BasicDBObject reset = new BasicDBObject();
reset.put("$set", new BasicDBObject("score", 0));
DBObject dbObj = queue.findAndModify(null, new BasicDBObject("score", -1), reset);
Run Code Online (Sandbox Code Playgroud)

所有这些都按预期工作!但是,稍后,可能会再次找到相同的名称,并带有新的分数.当最后一段代码运行时,会创建一个具有不同分数的新项目,这正是我不想要的:

BasicDBObject queueable = new BasicDBObject();
queueable.put("name", "Could be Bob again, could be someone new");
queueable.put("score", 1234);
queue.insert(queueable);
Run Code Online (Sandbox Code Playgroud)

如果我搜索Bob,我会发现:

{ "_id" : ObjectId("4f5e865ad6d09315326ea0f0"), "score" : 0, "name" : "Bob" }
{ "_id" : ObjectId("4f5e8691d6d09315326ea190"), "name" : "Bob", "score" : 886 }
Run Code Online (Sandbox Code Playgroud)

已创建第二条记录,再次获得更高分.字段的顺序是否重要?它似乎不应该,我不知道如何控制它.

Fap*_*iko 7

完成此操作的最佳方法是使用以下命令在名称字段上设置唯一索引:

db.foo.ensureIndex({name:1}, {unique:true});
Run Code Online (Sandbox Code Playgroud)

这会导致Sarah在进行插入调用时不会更新,因为它会找到另一条记录,其中已经为名称字段设置了"Sarah".


Tri*_*lpe 6

Your ensureIndex() call is not quite right. The first argument to ensureIndex() contains the keys that should be indexed; index options (like uniqueness) can be passed as the second argument. See the Java driver docs.

What you've got right now is attempting to create a non-unique index on two fields: "name" and "unique".

Try this to create your index:

DBObject nameIndex = new BasicDBObject();
nameIndex.put("name",1);

DBObject nameIndexOptions = new BasicDBObject();
nameIndexOptions.put("unique", true);

queue.ensureIndex(nameIndex, nameIndexOptions);
Run Code Online (Sandbox Code Playgroud)