简体   繁体   English

MongoDb 的 $set 等效于其 java 驱动程序

[英]MongoDb's $set equivalent in its java Driver

Is there a way in which I can modify the value of one of the keys in MongoDb via its Java Driver.有没有一种方法可以通过其 Java 驱动程序修改 MongoDb 中的一个键的值。 I tried out the following:我尝试了以下方法:

someCollection.update(DBObject query, DBObject update);
someCollection.findAndModify(DBObject query, DBObject update);

But both the functions completely replace the queried document with the updated document.但是这两个函数都用更新的文档完全替换了查询的文档。 What is the way to update only one of the value of a particular key as in the case of using $set in the mongo shell.(apart from making a completely new Document with all fields copied and one of the fields updated).在 mongo shell 中使用 $set 的情况下,仅更新特定键的一个值的方法是什么(除了制作一个全新的文档,其中复制了所有字段并更新了其中一个字段)。

BasicDBObject carrier = new BasicDBObject();
BasicDBObject query = new BasicDBObject();
query.put("YOUR_QUERY_STRING", YOUR_QUERY_VALUE);

BasicDBObject set = new BasicDBObject("$set", carrier);
carrier.put("a", 6);
carrier.put("b", "wx1");        
myColl.updateMany(query, set);

This should work, the answer which is accepted is not right above.这应该有效,被接受的答案不在上面。

Try something like this:尝试这样的事情:

BasicDBObject set = new BasicDBObject("$set", new BasicDBObject("age", 10));
set.append("$set", new BasicDBObject("name", "Some Name"));
someCollection.update(someSearchQuery, set);

Also look at this example .也看看这个例子

None of the solutions mentioned above worked for me.上面提到的解决方案都不适合我。 I realized that the query should be a Document type and not a BasicDBObject :我意识到查询应该是 Document 类型而不是 BasicDBObject :

Document set = new Document("$set", new Document("firstName","newValue"));

yourMongoCollection.updateOne(new Document("_id",objectId), set);

Where "yourMongoCollection" is of type "MongoCollection" and "objectId" of type "ObjectId"其中“yourMongoCollection”是“MongoCollection”类型,“objectId”是“ObjectId”类型

First, unless I want to reconfigure/reformat/"re-type" my values I'd go only with findAndModify and not update .首先,除非我想重新配置/重新格式化/“重新输入”我的值,否则我只会使用findAndModify不是update

Here is a fully working example for c&p purposes... Enjoy:这是用于 c&p 目的的完整工作示例......享受:

    Boolean updateValue(DB db, DBCollection collection, String id, String key, Object newValue)
    {
        DBCollection collection = db.getCollection(<collection name>);

        // Identify your required document (id, key, etc...)
        DBObject     query      = new BasicDBObject("_ID",<ID or key value>);
        DBObject     update     = new BasicDBObject("$set", new BasicDBObject(key, newValue));

        //These flags will guarantee that you'lls get the updated result
        DBObject     result     = collection.findAndModify(query, null, null, false, update,true, true);

        //Just for precaution....
        if(result == null)
            return false;

        return result.get(key).equals(newValue);
    }

The previous answer pointed me in the right direction, but the code to add a 2nd object to the update did not work for me.上一个答案为我指明了正确的方向,但是向更新添加第二个对象的代码对我不起作用。 The following did:做了以下事情:

BasicDBObject newValues = new BasicDBObject("age", 10);
newValues.append("name", "Some Name");
BasicDBObject set = new BasicDBObject("$set", newValues);
collection.update(someSearchQuery, set);

According to the documents, $set is an alise for $addFields , so just use that:根据文档, $set$addFields ,所以只需使用它:

        var iterable = collection.aggregate(Arrays.asList(
                Aggregates.addFields(new Field("foo", "bar"))
        ));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM