繁体   English   中英

Mongoose查询带两个arrays

[英]Mongoose query with two arrays

返回“all”数组中的值的查询是什么,排除了“friends”数组中的值

const userSchema = new Schema({
  all: [],
  friends:[],
 .... 
});

all = [ObjectId('65ss135442890aa'), ObjectId('65ss135442890dd')];

friends =[ObjectId('65ss135442890aa')];

查询结果应为 <ObjectId('65ss135442890dd')>

一种选择是使用$setDifference

db.collection.aggregate([
  {$project: {
      all: {$setDifference: ["$all", "$friends"]},
      _id: 0
    }
  }
])

playground 示例中查看它是如何工作的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM