Improve querying fields exist in MongoDB

前端 未结 4 418
挽巷
挽巷 2020-12-05 20:03

I\'m in progress with estimation of MongoDB for our customers. Per requirements we need associate with some entity ent variable set of name-value pairs.

4条回答
  •  隐瞒了意图╮
    2020-12-05 21:00

    You can redesign your schema like this:

    {
      pairs:[
      {k: "a", v: 5775},
      {k: "b", v: "b1"},
      ]
    }
    

    Then you indexing your key:

    db.people.ensureIndex({"pairs.k" : 1})
    

    After this you will able to search by exact match:

    db.ent.find({'pairs.k':"a"})
    

    In case you go with Sparse index and your current schema, proposed by @WesFreeman, you will need to create an index on each key you want to search. It can affect write performance or will be not acceptable if your keys are not static.

提交回复
热议问题