0

My collection looks like:

{
"_id" : ObjectId("520c8976586f12c80900301d"),
"IDENTITY" : "ae4ff1546ecfe6ce6e8ebb2d870d57fb",
"NAME" : "Winmain Yao",
"TYPE" : "COMPANY",
"PUBLIC" : 1,
"BACKGROUND" : null,
"CONTACT" : {
  "PHONE" : {
    "WORK" : "18630283895",
    "HOME" : "188837283"
},
"EMAIL" : {
  "DEFAULT00001" : "zxwinmain@gmail.com"
},
"IM" : [],
"SITE" : [],
"ADDRESS" : [],
"SOCIAL" : []
},
"RELATION" : {
  "ACCOUNT" : "16713a29767fa4e8f139dfb249900c07",
  "USER" : "84a9e7a8e5f3a13d411cfa8ff4a62a50"
},
"CREATED" : 1376553334,
"UPDATED" : 0,
"DELETED" : 0
}

Now I want to get the customer who's phone = 18630283895, how can I do that?

Thanks a lot.

4

1 回答 1

0

以下:

"PHONE" : {
    "WORK" : "18630283895",
    "HOME" : "188837283"
},

错误的做法。由于您总是会有更多类型的电话号码,因此您最终得到了一个“实际上是一个值的键”。这使得查询(和索引)变得困难。更好的方法是:

"PHONE" : [
    { type: "WORK", number: "18630283895" },
    { type: "HOME", number: "188837283" },
],

然后你可以很容易地索引它:

db.collection.ensureIndex( { "PHONE.number": 1 } );

并通过以下方式查询数字:

db.collection.find( { "PHONE.number" : "18630283895" } );
于 2013-08-15T08:43:01.503 回答