How to Create a nested index in MongoDB?

后端 未结 3 775
暖寄归人
暖寄归人 2020-12-14 05:37

A. How do I index nested and all of it\'s values?

B. How do I index valuetwo?

{
    id: 00000,
    attrs: {
        nested:{
         


        
3条回答
  •  借酒劲吻你
    2020-12-14 06:36

    MongoDB automatically creates a multikey index if any indexed field is an array; you do not need to explicitly specify the multikey type.

    This will work for both the scenario's db.coll.createIndex( { "addr.pin": 1 } )

    Scenario 1 nested OBJECTS

    {
      userid: "1234",
      addr: {
        pin:"455522"
      }
    },
    {
      userid: "1234",
      addr: {
        pin:"777777"
      }
    }
    

    Scenario 2 nested Arrays

    {
      userid: "1234",
      addr: [
        { pin:"455522" },
        { pin:"777777" },
      ]
    }
    

    https://docs.mongodb.com/manual/core/index-multikey/

提交回复
热议问题