How to check if an array field contains a unique value or another array in MongoDB?

后端 未结 2 718
夕颜
夕颜 2020-12-02 04:16

I am using mongodb now.

I have a blogpost collection, and it has a tags field which is an array, e.g.

blogpost1.tags = [\'tag1\', \'tag2\'         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 04:26

    Try this out:

    db.blogpost.find({ 'tags' : 'tag1'}); //1
    db.blogpost.find({ 'tags' : { $all : [ 'tag1', 'tag2' ] }}); //2
    db.blogpost.find({ 'tags' : { $in : [ 'tag3', 'tag4' ] }}); //3
    

提交回复
热议问题