MongoDB检查存在于数组中

时间:2011-07-18 14:29:26

标签: mongodb

收集'书籍':

array (
  '_id' => new MongoId("4e242b0ea5bb1bb00b000000"),
  'book' => 
  array (
    '0' => 1,
    '1' => 2,
    '2' => 3,
    '3' => 14,
    '4' => 15,
  ),
  'book_count' => 5,
  'user_name' => 'john',
)

如何查看数组“书籍”中“某本书”的位置?

这是检查的唯一方法吗?

db.books.count({user_name:'john', book:1})

1 个答案:

答案 0 :(得分:5)

是的,虽然以下会更快

db.books.find({user_name:'john', book:1}).limit(1) != null