简体   繁体   English

如何从 mongoose 查找中排除对象数组?

[英]How to exclude an array of objects from mongoose find?

Lets pretend my collection holds following elements:假设我的收藏包含以下元素:

const Item = mongoose.Schema({
   type: { type: String },
   group: { type: Number }
});

[
   { type: "A", group: 1 },
   { type: "B", group: 1 },
   { type: "C", group: 1 },
   { type: "A", group: 2 },
   { type: "B", group: 2 },
   { type: "C", group: 2 },
]

Now I want to execute a find operation but want to exclude following items:现在我想执行一个find操作,但想排除以下项目:

const items = [
    { type: "A", group: 1 },
    { type: "B", group: 1 },
];

const result = await Model.find({ /* ??? */ });

How does the query have to look to get the following response?查询必须如何查看才能获得以下响应?

const result = [
   { type: "C", group: 1 },
   { type: "A", group: 2 },
   { type: "B", group: 2 },
   { type: "C", group: 2 },
];

Try $nor operator, performs a logical NOR operation on an array of one or more query expression and selects the documents that fail all the query expressions in the array.尝试$nor运算符,对包含一个或多个查询表达式的数组执行逻辑 NOR 运算,并选择数组中所有查询表达式都失败的文档。

const items = [
    { type: "A", group: 1 },
    { type: "B", group: 1 },
];
const result = await Model.find({ $nor: items });

Playground操场

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

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