通过 Java API 将具有值的字段添加到 MongoDB 中的现有文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20989389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add field with value to existing document in MongoDB via Java API
提问by rok
The following code haven't worked for me:
以下代码对我不起作用:
public void addFieldWithValueToDoc(String DBName, String collName, String docID, String key, String value) {
BasicDBObject setNewFieldQuery = new BasicDBObject().append("$set", new BasicDBObject().append(key, value));
mongoClient.getDB(DBName).getCollection(collName).update(new BasicDBObject().append("_id", docID), setNewFieldQuery);
}
Where mongoClient variable's type is MongoClient.
其中 mongoClient 变量的类型是 MongoClient。
It's inspired by Add new field to a collection in MongoDB. What's wrong and how to do it right? Thanks.
它的灵感来自将新字段添加到 MongoDB 中的集合。出了什么问题以及如何正确处理?谢谢。
采纳答案by Trisha
I've written a JUnit test to prove that your code does work:
我编写了一个 JUnit 测试来证明您的代码确实有效:
@Test
public void shouldUpdateAnExistingDocumentWithANewKeyAndValue() {
// Given
String docID = "someId";
collection.save(new BasicDBObject("_id", docID));
assertThat(collection.find().count(), is(1));
// When
String key = "newKeyName";
String value = "newKeyValue";
addFieldWithValueToDoc(db.getName(), collection.getName(), docID, key, value);
// Then
assertThat(collection.findOne().get(key).toString(), is(value));
}
public void addFieldWithValueToDoc(String DBName, String collName, String docID, String key, String value) {
BasicDBObject setNewFieldQuery = new BasicDBObject().append("$set", new BasicDBObject().append(key, value));
mongoClient.getDB(DBName).getCollection(collName).update(new BasicDBObject().append("_id", docID), setNewFieldQuery);
}
So your code is correct, although I'd like to point out some comments on style that would make it more readable:
所以你的代码是正确的,虽然我想指出一些关于风格的评论,这将使它更具可读性:
- Parameters and variables should start with a lower-case letter.
DBName
should bedbName
, - You don't need
new BasicDBObject().append(key, value)
usenew BasicDBObject(key, value)
- 参数和变量应以小写字母开头。
DBName
应该是dbName
, - 你不需要
new BasicDBObject().append(key, value)
使用new BasicDBObject(key, value)
This code does the same thing as your code, but is shorter and simpler:
此代码与您的代码执行相同的操作,但更短更简单:
public void addFieldWithValueToDoc(String dbName, String collName, String docID, String key, String value) {
mongoClient.getDB(dbName).getCollection(collName).update(new BasicDBObject("_id", docID),
new BasicDBObject("$set", new BasicDBObject(key, value)));
}
回答by invzbl3
To update existing documents in a collection, you can use the collection's updateOne() or updateMany methods.
要更新集合中的现有文档,您可以使用集合的 updateOne() 或 updateMany 方法。
updateOne method has the following form:
updateOne 方法具有以下形式:
db.collection.updateOne(filter, update, options)
filter- the selection criteria for the update. The same query selectors as in the find() method are available.
Specify an empty document { } to update the first document returned in the collection.
update- the modifications to apply.
filter- 更新的选择标准。可以使用与 find() 方法中相同的查询选择器。
指定一个空文档 { } 以更新集合中返回的第一个文档。
更新- 要应用的修改。
So, if you want to add one more field using Mongodb Java driver 3.4+, it will be:
因此,如果您想使用 Mongodb Java 驱动程序 3.4+ 再添加一个字段,它将是:
collection.updateOne(new Document("flag", true),
new Document("$set", new Document("title", "Portable Space Ball")));
The following operation updates a single document where flag:true
以下操作更新单个文档,其中 flag:true
Or in the same logic:
或者以相同的逻辑:
collection.updateOne(eq("flag", true),
new Document("$set", new Document("title", "Portable Space Ball")));
If the title
field does not exist, $set
will add a new field with the specified value, provided that the new field does not violate a type constraint. If you specify a dotted path for a non-existent field, $set
will create the embedded documents as needed to fulfill the dotted path to the field.
如果title
字段不存在,$set
将添加具有指定值的新字段,前提是新字段不违反类型约束。如果您为不存在的字段指定虚线路径,$set
将根据需要创建嵌入文档以实现该字段的虚线路径。