根据MongoDB集合中先前记录的匹配更新新文档字段值

时间:2017-02-21 13:34:01

标签: python mongodb pymongo

我在mongodb集合中有以下文档:

记录ids

后,1-4 4-8的记录会被插入到集合中
[
    {id: "1" , name: "archie", age: "25", nationality: "british"},
    {id: "2" , name: "archie", age: "25", nationality: "british"},
    {id: "3" , name: "john", age: "25", nationality: "british"},
    {id: "4" , name: "alex", age: "27", nationality: "french"},
    {id: "5" , name: "archie", age: "25", nationality: "british", tag: "value2"},
    {id: "6" , name: "archie", age: "25", nationality: "british", tag: "value2"},
    {id: "7" , name: "john", age: "25", nationality: "british"},
    {id: "8" , name: "alex", age: "27", nationality: "french", tag: "value1"}
]

我想在tagnameage匹配的先前记录中使用相同的值添加/更新字段nationality

这是预期的结果:

[
    {id: "1" , name: "archie", age: "25", nationality: "british", tag: "value2"},
    {id: "2" , name: "archie", age: "25", nationality: "british", tag: "value2"},
    {id: "3" , name: "john", age: "25", nationality: "british"},
    {id: "4" , name: "alex", age: "27", nationality: "french", tag: "value1"},
    {id: "5" , name: "archie", age: "25", nationality: "british", tag: "value2"},
    {id: "6" , name: "archie", age: "25", nationality: "british", tag: "value2"},
    {id: "7" , name: "john", age: "25", nationality: "british"},
    {id: "8" , name: "alex", age: "27", nationality: "french", tag: "value1"}
]

实现这一目标的最佳方法是什么?

(我正在使用PyMongo)

非常感谢,

2 个答案:

答案 0 :(得分:1)

要使用$update和多个

实现此$set查询

让我拥有一个包含以下数据的集合 - 所有这些查询都可以在MongoShell中使用

db.collection.insert([{id: "1" , name: "archie", age: "25", nationality: "british"}, {id: "2" , name: "archie", age: "25", nationality: "british"}, {id: "3" , name: "john", age: "25", nationality: "british"},     {id: "4", name: "alex", age: "27", nationality: "french"}]);

插入后的集合中的文档

db.collection.find().pretty()
{
        "_id" : ObjectId("58ac57e742616c14fcf43aaf"),
        "id" : "1",
        "name" : "archie",
        "age" : "25",
        "nationality" : "british"
}
{
        "_id" : ObjectId("58ac57e742616c14fcf43ab0"),
        "id" : "2",
        "name" : "archie",
        "age" : "25",
        "nationality" : "british"
}
{
        "_id" : ObjectId("58ac57e742616c14fcf43ab1"),
        "id" : "3",
        "name" : "john",
        "age" : "25",
        "nationality" : "british"
}
{
        "_id" : ObjectId("58ac57e742616c14fcf43ab2"),
        "id" : "4",
        "name" : "alex",
        "age" : "27",
        "nationality" : "french"
}

更新查询

db.collection.update({name:"archie", age:"25", nationality:"british"}, {$set:{tag:"value2"}}, {multi:true});

请注意,我使用了$ set和multi。

$ set - 仅设置现有值的新标签

multi - 更新多个文档

执行更新查询后

db.collection.find().pretty()


{
        "_id" : ObjectId("58ac57e742616c14fcf43aaf"),
        "id" : "1",
        "name" : "archie",
        "age" : "25",
        "nationality" : "british",
        "tag" : "value2"
}
{
        "_id" : ObjectId("58ac57e742616c14fcf43ab0"),
        "id" : "2",
        "name" : "archie",
        "age" : "25",
        "nationality" : "british",
        "tag" : "value2"
}
{
        "_id" : ObjectId("58ac57e742616c14fcf43ab1"),
        "id" : "3",
        "name" : "john",
        "age" : "25",
        "nationality" : "british"
}
{
        "_id" : ObjectId("58ac57e742616c14fcf43ab2"),
        "id" : "4",
        "name" : "alex",
        "age" : "27",
        "nationality" : "french"
}

仅更新名称,年龄和国籍相同的所有文件。

答案 1 :(得分:0)

您可以根据现有行准备标记值:

> db.coll.insert([     {id: "5" , name: "archie", age: "25", nationality: "british", tag: "value2"},     {id: "6" , name: "archie", age: "25", nationality: "british", tag: "value2"},     {id: "7" , name: "john", age: "25", nationality: "british"},     {id: "8" , name: "alex", age: "27", nationality: "french", tag: "value1"} ]);
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 4,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})
> var obIns = {id: "1" , name: "archie", age: "25", nationality: "british"};
> db.coll.find({name:obIns.name, nationality: obIns.nationality}).limit(1).forEach(function(o){obIns.tag = o.tag;db.coll.insert(obIns);});
> db.coll.find();
{ "_id" : ObjectId("58ac489e9daf1be6a0d0456a"), "id" : "5", "name" : "archie", "age" : "25", "nationality" : "british", "tag" : "value2" }
{ "_id" : ObjectId("58ac489e9daf1be6a0d0456b"), "id" : "6", "name" : "archie", "age" : "25", "nationality" : "british", "tag" : "value2" }
{ "_id" : ObjectId("58ac489e9daf1be6a0d0456c"), "id" : "7", "name" : "john", "age" : "25", "nationality" : "british" }
{ "_id" : ObjectId("58ac489e9daf1be6a0d0456d"), "id" : "8", "name" : "alex", "age" : "27", "nationality" : "french", "tag" : "value1" }
{ "_id" : ObjectId("58ac48b49daf1be6a0d0456e"), "id" : "1", "name" : "archie", "age" : "25", "nationality" : "british", "tag" : "value2" }