如何增加MongoDB中的现有值 [英] How to increment existing value in MongoDB
问题描述
我正在使用 MongoDB的Stitch平台.我想在数据库中存储与该值关联的 value
和 count
.现在 value
可能不是第一次出现,所以我想用 count = 1
插入 value
.我可以使用 update()
使用 $ inc
更新计数的现有值,也可以使用 upsert()
将值添加到数据库.现在,事情是,我有了我的值和计数的 map
,我想一次全部插入(更新/更新).我不想给网络增加负担.我当时使用 insertMany()
一次插入 map
,但显然不会更新该值.
I am using Stitch platform by MongoDB. I want to store a value
and a count
associated with that value in the database. Now the value
may not be present for the first time, so I would like to insert the value
with count = 1
.
I can use update()
to update the existing value of count using $inc
or I can use upsert()
to add the value in the database.
Now, the thing is, I have a map
of my values and the count, and I want to insert(update/upsert) all at once. I don't want to put a load on the network.
I was using insertMany()
to insert the map
at once but it clearly doesn't updates the value.
那么有可能这样做吗?
P.S.我使用的是javascript.
P.S. I am using javascript for the same.
推荐答案
根据MongoDb 3.6:
db.collection.update(查询,更新,选项)
修改集合中的一个或多个现有文档.该方法可以修改一个或多个现有文档的特定字段,或完全替换一个现有文档,具体取决于更新参数.
Modifies an existing document or documents in a collection. The method can modify specific fields of an existing document or documents or replace an existing document entirely, depending on the update parameter.
意思是您可以使用update上载多个文档.
The meaning is that you can upsert multiple documents using update.
首先,您应该从地图中创建仅包含值的数组.
First you should create array from your map that contains only the value.
const arrayOfValues = ['value_01', 'values_02'];
然后,您应该在更新方法上使用upsert + multi选项:
Then you should use the upsert + multi options on the update method:
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 })
希望这很有帮助:)
这篇关于如何增加MongoDB中的现有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!