更新字段我想更新所有電子郵件的收集場profile.email [0]。地址只是像這樣在SQL:插入和MongoDB中
update dbo.users
set profile.email=emials.[0].address
我知道這個愚蠢的代碼獲得了」 t也可以用於SQL。只是爲了讓我明白我需要什麼。
它是這樣的,但是不工作:
db.getCollection('users').update(
{_id: "QYoHQkTuTXnEC6Pws"},
{$set:
{'profile.email': db.getCollection('users').aggregate({$match: {_id:'QYoHQkTuTXnEC6Pws'}},
{$project: {_id:0,email: {$arrayElemAt:
['$emails.address',0]}}})
}
}
)
的結果是不正確的:
"email" : {
"_batch" : [
{
"email" : "[email protected]"
}
],
"_useReadCommands" : true,
"_cursorid" : NumberLong(0),
"_batchSize" : undefined,
"_ns" : "meteor.users",
"_db" : {
"_mongo" : {
"slaveOk" : true,
"host" : "localhost:3001",
"defaultDB" : "",
"_readMode" : "commands",
"_writeMode" : "commands"
},
"_name" : "meteor"
},
"_collName" : "users",
"_cursorHandle" : {}
}
你的mongo版本是什麼? – sidgate
mongo版本是3.4.0 – Mehr