使用正则表达式查找不包含ingredients
牛奶或巧克力的文档(使用此处的技术),然后用 a 否定它就可以了$not
:
db.recipes.find({'ingredients.name': {$not: /^((?!(milk|chocolate)).)*$/}})
测试:
{
"_id": ObjectId("5114f7b748994465c5b5c369"),
"name": "Recipe 1",
"ingredients": [
{
"name": "dark chocolate",
"qty": "1 g"
},
{
"name": "milk",
"qty": "2 g"
},
{
"name": "unsalted butter",
"qty": "3 g"
}
]
}
{
"_id": ObjectId("5114f9f348994465c5b5c36a"),
"name": "Recipe 2",
"ingredients": [
{
"name": "dark chocolate",
"qty": "1 g"
},
{
"name": "milk",
"qty": "2 g"
}
]
}
{
"_id": ObjectId("5114fcec48994465c5b5c36b"),
"name": "Recipe 3",
"ingredients": [
{
"name": "milk chocolate",
"qty": "1 g"
}
]
}
{
"_id": ObjectId("5114fd0e48994465c5b5c36c"),
"name": "Recipe 4",
"ingredients": [
{
"name": "soy",
"qty": "1 g"
}
]
}
{
"_id": ObjectId("5114fd5248994465c5b5c36d"),
"name": "Recipe 5",
"ingredients": [
{
"name": "chocolate mud",
"qty": "1 g"
}
]
}
它输出:
{
"_id": ObjectId("5114f9f348994465c5b5c36a"),
"name": "Recipe 2",
"ingredients": [
{
"name": "dark chocolate",
"qty": "1 g"
},
{
"name": "milk",
"qty": "2 g"
}
]
}
{
"_id": ObjectId("5114fcec48994465c5b5c36b"),
"name": "Recipe 3",
"ingredients": [
{
"name": "milk chocolate",
"qty": "1 g"
}
]
}
{
"_id": ObjectId("5114fd5248994465c5b5c36d"),
"name": "Recipe 5",
"ingredients": [
{
"name": "chocolate mud",
"qty": "1 g"
}
]
}