MongoDB - 检查集合中是否存在多个字段

时间:2016-07-12 04:24:55

标签: mongodb

我正在寻找一种方法来查看集合中是否已经存在两个字段。我一直在阅读文档,但它们似乎没有太多帮助。 我已经看到你可以使用$ exists但是这可以同时比较多个字段吗?

我将如何实现这一目标?

我有一个类似这样的集合

{userId: ObjectId("57840667f862724c0f736a69"), artId: ObjectId("5783e368b30fb4482ba390eb")}

我想检查一下这个集合是否包含

userId:ObjectId("57840667f862724c0f736a69") && artId: ObjectId("5783e368b30fb4482ba390eb") 

1 个答案:

答案 0 :(得分:4)

我认为您可以使用and$exist

db.collection.find({
  $and : [
    { userId: { $exists: true } },
    { artId : { $exists: true } }
  ]
})
相关问题