如何使用另一个集合中的字段值更新Mongodb集合中的文档

时间:2017-01-31 07:44:43

标签: javascript mongodb mongodb-query

我有两个系列 1)Floorplan_backup。   该文件的结构如下: -

select f1.Name, f1.Age, f4.LastProductName from Employee f1
cross apply 
(
    select top 1 f2.Name as LastProductName, count(distinct f3.productID) over(partition by f3.EmployeeID) NbProduct 
    from Product f2 inner join ProductEmployee f3 on f2.ID=f3.ProductID
    where f3.EmployeeID=f1.ID
    order by f3.Date desc
) f4
where f1.Age>50 and f4.NbProduct>=10

2)平面图。文件的结构

{
    "_id" : ObjectId("5877e18a88db272b578572bd"),
    "floorplans" : [ 
        {
            "name" : "Campus Center - Atrium",
            "uuid" : NumberLong(3),
            "cameras" : []
        }, 
        {
            "name" : "Campus Center - Bridge ",
            "uuid" : NumberLong(4),
            "cameras" : []
        }, 
        {
            "name" : "Campus Center - Top",
            "uuid" : NumberLong(5),
            "cameras" : []
        }, 
        {
            "name" : "Performance Dining Hall",
            "uuid" : NumberLong(6),
            "cameras" : []
        }, 
        {
            "name" : "Main Kitchen",
            "uuid" : NumberLong(7),
            "cameras" : []
        }, 
        {
            "name" : "OrgChart - Board of Trustees",
            "uuid" : NumberLong(8),
            "cameras" : []
        }, 
        {
            "name" : "OrgChart - Academic Affairs",
            "uuid" : NumberLong(9),
            "cameras" : []
        }, 
        {
            "name" : "OrgChart - Admissions",
            "uuid" : NumberLong(10),
            "cameras" : []
        }, 
        {
            "name" : "OrgChart - Athletics",
            "uuid" : NumberLong(11),
            "cameras" : []
        }, 
        {
            "name" : "OrgChart - Financial",
            "uuid" : NumberLong(14),
            "cameras" : []
        }, 
        {
            "name" : "OrgChart - HR",
            "uuid" : NumberLong(15),
            "cameras" : []
        }, 
        {
            "name" : "OrgChart - Institutional",
            "uuid" : NumberLong(16),
            "cameras" : []
        }, 
        {
            "name" : "OrgChart - IT",
            "uuid" : NumberLong(17),
            "cameras" : []
        }, 

        {
            "name" : "OrgChart - Student Success",
            "uuid" : NumberLong(20),
            "cameras" : []
        }, 
        {
            "name" : "OrgChart - Student Life",
            "uuid" : NumberLong(21),
            "cameras" : []
        }
    ],
    "userid" : "user-56cb3c4c0c953470865336",
    "firstviewhost" : undefined
}

我想更新Floorpla_backup中的firstviewhost,其值为" dean.vizsafe.com"通过匹配两个集合的用户ID。

使用的javascript代码是: -

{
    "_id" : ObjectId("589025b03422cafc09913363"),
    "userid" : "user-56cb3c4c0c953470865336",
    "firstviewhost" : "dean.vizsafe.com"
}

但是,是不是给了我想要的结果集。你能让我知道这有什么问题吗?

2 个答案:

答案 0 :(得分:1)

doc2是一个数组。使用findOne获取对象。

<nul >"file.txt" set/p="""

答案 1 :(得分:0)

find返回光标,而不是使用findOne。

db.floorplan_backup.find().forEach(function (doc1) {
    var doc2 = db.floorplan.findOne({ userid: doc1.userid }, { firstviewhost: 1 });
    if (doc2 != null) {
        doc1.firstviewhost = doc2.firstviewhost;
        db.floorplan_backup.save(doc1);
    }
});