![](/img/trans.png)
[英]Trying to insert few records (Key and values) in collection (mongodb) if particular (Key:value) is not present using python
[英]How to check if a key value pair is matching in one collection and update the value in the key present in another mongodb collection using python
收藏一:
username:"abc123"
收藏2:
username:"abc123"
_id:ObjectId("5e78c5794f1f2d69b2a08139")
如果集合 1 和集合 2 中的“用戶名”匹配,我應該能夠從集合 2 復制“_id”並將新鍵和值插入為“代理:
ObjectId("5e78c5794f1f2d69b2a08139"
聚合可以做到這一點。
在集合 2 上運行聚合:
db.getCollection("Collection 2").aggregate([
{$lookup: {
from: "Collection 1",
localField: "username",
foreignField: "username",
as: "lookedup"
}},
{$unwind: "$lookedup"},
{$addFields: {
"lookedup.agent": "$_id"
}},
{$replaceRoot: {newRoot: "$lookedup"}},
{$merge: "Collection 1"}
])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.