[英]Mongoose query with two arrays
What is the query that returns the values in 'all' array, excluded the values in 'friends' array
返回“all”数组中的值的查询是什么,排除了“friends”数组中的值
const userSchema = new Schema({
all: [],
friends:[],
....
});
all = [ObjectId('65ss135442890aa'), ObjectId('65ss135442890dd')];
friends =[ObjectId('65ss135442890aa')];
result of query should be <ObjectId('65ss135442890dd')>
查询结果应为 <ObjectId('65ss135442890dd')>
One option is to use $setDifference
:一种选择是使用
$setDifference
:
db.collection.aggregate([
{$project: {
all: {$setDifference: ["$all", "$friends"]},
_id: 0
}
}
])
See how it works on the playground example在playground 示例中查看它是如何工作的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.