【问题标题】:How to increment existing value in MongoDB如何增加 MongoDB 中的现有值
【发布时间】:2017-12-28 11:34:24
【问题描述】:

我正在使用Stitch platform by MongoDB。我想在数据库中存储与该值关联的valuecount。现在value 可能不是第一次出现,所以我想插入valuecount = 1。 我可以使用update() 使用$inc 更新count 的现有值,或者我可以使用upsert() 在数据库中添加值。 现在,问题是,我有一个map 我的值和计数,我想一次插入(更新/更新)。我不想给网络增加负担。 我使用insertMany() 一次插入map,但它显然不会更新值。

那么有可能吗?

附:我也在使用 javascript。

【问题讨论】:

    标签: javascript mongodb mongodb-stitch


    【解决方案1】:

    根据 MongoDb 3.6:

    db.collection.update(query, update, options)

    修改集合中的一个或多个现有文档。该方法可以修改现有文档的特定字段或完全替换现有文档,具体取决于更新参数。

    意思是你可以使用update来upsert多个文档。

    首先,您应该从只包含值的地图创建数组。

    const arrayOfValues = ['value_01', 'values_02'];
    

    那么你应该在更新方法上使用 upsert + multi 选项:

    db.foo.update({value: { $in: arrayOfValues}}, {$inc: {count:1}}, { upsert: true, multi: true });
    

    测试输出:

    > db.createCollection("test");
    { "ok" : 1 }
    > db.test.insertMany([{value: "a"}, {value: "b"}, {value: "c"}];
    ... );
    2017-12-31T12:12:18.040+0200 E QUERY    [thread1] SyntaxError: missing ) after argument list @(shell):1:61
    > db.test.insertMany([{value: "a"}, {value: "b"}, {value: "c"}]);
    {
        "acknowledged" : true,
        "insertedIds" : [
            ObjectId("5a48b8061b98cc5ac252e435"),
            ObjectId("5a48b8061b98cc5ac252e436"),
            ObjectId("5a48b8061b98cc5ac252e437")
        ]
    }
    > db.test.find();
    { "_id" : ObjectId("5a48b8061b98cc5ac252e435"), "value" : "a" }
    { "_id" : ObjectId("5a48b8061b98cc5ac252e436"), "value" : "b" }
    { "_id" : ObjectId("5a48b8061b98cc5ac252e437"), "value" : "c" }
    > db.test.update({value: { $in: ["a", "b", "c"]}}, {$inc: {count:1}}, { upsert: true, multi: true });
    WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
    > db.test.find();
    { "_id" : ObjectId("5a48b8061b98cc5ac252e435"), "value" : "a", "count" : 1 }
    { "_id" : ObjectId("5a48b8061b98cc5ac252e436"), "value" : "b", "count" : 1 }
    { "_id" : ObjectId("5a48b8061b98cc5ac252e437"), "value" : "c", "count" : 1 }
    > db.test.update({value: { $in: ["a", "b", "c"]}}, {$inc: {count:1}}, { upsert: true, multi: true });
    WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
    > db.test.find();
    { "_id" : ObjectId("5a48b8061b98cc5ac252e435"), "value" : "a", "count" : 2 }
    { "_id" : ObjectId("5a48b8061b98cc5ac252e436"), "value" : "b", "count" : 2 }
    { "_id" : ObjectId("5a48b8061b98cc5ac252e437"), "value" : "c", "count" : 2 }
    > db.test.update({value: { $in: ["a", "b", "c"]}}, {$inc: {count:1}}, { upsert: true, multi: true });
    WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
    

    希望对你有帮助:)

    【讨论】:

    • 实际上,我正在使用 javascript,所以如果我能知道如何在 javascript 中做到这一点,那就太好了。不过没关系,至少现在我知道从哪里开始了。
    • 你能提供你的答案的来源吗。这真的很有帮助。
    • @Yochai_Akoka 我尝试运行此命令db.test.update({value: {$in: ["b","a"]}}, {$inc: {count:1}}, {upsert: true, multi: true}),但它不会在数据库中更新任何内容。我的数据库已经有value: "a"count:1,我想用count:1 插入value:"b" 并增加value:"a" 的计数。它只会增加数据库中已经存在的值的数量。
    • 我在我的答案中还添加了来自我的控制台的测试输出。
    猜你喜欢
    • 1970-01-01
    • 2020-09-22
    • 2019-08-08
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    相关资源
    最近更新 更多