MongoDB Java API Query UPDATE include word



>我有一个问题。我需要在字段中的值包含一个单词:"名称"。例如:

    {name : "Apple inc."}

我想加上"公司"一词。

{name : "Company Apple inc."}

但是,我不能。我的问题在哪里?

collectionCOMPANIES.updateMany(
        new Document("$where", "true")
            , 
            new Document("$set", new Document("name","Company + $name")
                    ));

如果我这样做,返回:

{name : "Company + $name"}

谢谢。

更新解决方案******

对于java中的最终代码,这是:

collectionCOMPANIES.find().forEach(new Block<Document>() {
        public void apply(final Document document) {
            final Document DocuNew = new Document();
            DocuNew.putAll(document);

            DocuNew.put("name", "Company " + document.get("name")); 

            newcollection.insertOne(DocuNew);
        }
    });

试试这个。

db.collectionCOMPANIES.find().snapshot().forEach( function (doc) {
doc.name = "Company " + doc.name; 
db.collectionCOMPANIES.save(doc); 
});

为此,我正在使用mongodb提供的snapshot。 你可以在这里阅读更多关于它的信息

最新更新