In MongoDB how do you index an embedded object fields in an array?

前端 未结 2 1241
暖寄归人
暖寄归人 2021-02-01 03:00

The mongodb documentation for multikeys gives an example of querying embedded object fields in an array:

http://www.mongodb.org/display/DOCS/Multikeys

But there\

相关标签:
2条回答
  • 2021-02-01 03:46

    you create the index as if you would with a "normal" field;

    db.[collection].ensureIndex( { [yourArrayField] : 1 } )
    
    0 讨论(0)
  • 2021-02-01 03:57

    You can create the following index :

    db.posts.ensureIndex({"comments.author" : 1})
    

    This will index only the author field of the embedded documents. Note that the index will be used for

    db.posts.find( { "comments.author" : "julie" } )
    

    As well as

    db.posts.find( { comments: {$elemMatch: {author : "julie" }}} )
    
    0 讨论(0)
提交回复
热议问题