我有三个收藏。如果要匹配,我想根据“手机号码”合并所有三个集合的文档,否则添加为新文档并将其保存到新集合中。
(1)用户集合
[
{id: ObjectId(...), name:"john", mobile:1234, city:"London"},
{id: ObjectId(...), name:"mike", mobile:1111, city:"Berlin"},
{id: ObjectId(...), name:"jenny", mobile:2222, city:"Paris"},
]
(2)爱好收藏
[
{id: ObjectId(...), hobby:"hockey", mobile:1111},
{id: ObjectId(...), hobby:"cricket", mobile:1234},
{id: ObjectId(...), hobby:"movies", mobile:6666},
]
(3)关键字集合
[
{id: ObjectId(...), keyword:"cr", mobile:1234},
{id: ObjectId(...), keyword:"hk", mobile:2222},
{id: ObjectId(...), keyword:"uk", mobile:7777}
]
尽管我能够将所有三个收藏文档合并为一个。
db.finalCollection.save(db.users.find().toArray());
db.finalCollection.save(db.hobbies.find().toArray());
db.finalCollection.save(db.keyword.find().toArray());
但是我不知道如何合并具有相同手机号码的文档,并且在不匹配的情况下添加为新文档。
我的预期输出是:
[
{id: ObjectId(...), name:"john", mobile:1234, city:"London",
hobby:"cricket",keyword:"cr"},
{id: ObjectId(...), name:"mike", mobile:1111, city:"Berlin",
hobby:"hockey"},
{id: ObjectId(...), name:"jenny", mobile:2222, city:"Paris",
keyword:"hk"},
{id: ObjectId(...), hobby:"movies", mobile:6666},
{id: ObjectId(...), keyword:"uk", mobile:7777}
]