加入两个集合并在 MongoDB 中更新
Join two collectios and update in MongoDB
在 MongoDB 中是否有任何等同于以下 MYSQL 查询的内容?
update table_name1 as a
join table_name2 as b
on a.uniqueid=b.uniqueid
set a.column1=b.column1,a.column2=b.column2 ;
查找可能是一个可能的解决方案,但不知道如何使用它进行更新。
你可以这样做,
db.table1.find({}).forEach(function (t1) { // foreach on table one
var t2 = db.table2.findOne({ uniqueid: t1.uniqueid},
{ column1: 1,column2:1 }); // finding unique id of table1 with table2
if (t2 != null) { // checking if not null
t1.column1 = t2.column1; // assigning tabl2's value to table1
db.table1.save(t1); // save it
}
});
请删除我的评论
在 MongoDB 中是否有任何等同于以下 MYSQL 查询的内容?
update table_name1 as a
join table_name2 as b
on a.uniqueid=b.uniqueid
set a.column1=b.column1,a.column2=b.column2 ;
查找可能是一个可能的解决方案,但不知道如何使用它进行更新。
你可以这样做,
db.table1.find({}).forEach(function (t1) { // foreach on table one
var t2 = db.table2.findOne({ uniqueid: t1.uniqueid},
{ column1: 1,column2:1 }); // finding unique id of table1 with table2
if (t2 != null) { // checking if not null
t1.column1 = t2.column1; // assigning tabl2's value to table1
db.table1.save(t1); // save it
}
});
请删除我的评论