db.fellas.findOne() { "_id" : 123 "women" : [ 1, 12, 3-6ren">
gpt4 book ai didi

MongoDb - "join"或 $ 在两个集合中

转载 作者:可可西里 更新时间:2023-11-01 09:46:01 24 4
gpt4 key购买 nike

假设我有 2 个集合:

> db.fellas.findOne()
{
"_id" : 123
"women" : [
1,
12,
34
]
}

> db.women.findOne()
{ "_id" : 12, "busty" : 1 }

有些女人属于某个人,但有些则不属于。在上面的例子中,女人确实属于那个男人。

我如何找到不属于任何男人的所有女人?我尝试使用 $unwind 来做到这一点,但没有成功。我的意思是,我不知道我应该进一步做什么。

你的想法?

最佳答案

您可以使用如下函数:

db.women.find().forEach(function(w){
if( !db.fellas.findOne({ women:{$in:[w._id]} }) )
printjson(w);
)}

还有一种方式,就是mapreduce。

关于MongoDb - "join"或 $ 在两个集合中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13910902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com