mongoDB³£¼ûµÄ²éѯË÷Òý£¨Èý£©(ËÄ)

2015-07-24 06:20:58 ¡¤ ×÷Õß: ¡¤ ä¯ÀÀ: 15
("5572a965aba41684d6e8826d"), "w" : [ 1, 2 ] }

?

?
3. ʹÓÃgeoNear²éѯ
geoNearʹÓÃrunCommandÃüÁî½øÐÐʹÓÃ
db.runCommand(
    {getNear:,
    near:[x,y],
    minDistance:(¶Ô2dË÷ÒýÎÞЧ)
    maxDistance:
    num:
}
)

> db.runCommand({geoNear:"location",near:[1,2],maxDistance:10,num:1})
{
    "results" : [
        {
            "dis" : 0,
            "obj" : {
                "_id" : ObjectId("5572a965aba41684d6e8826d"),
                "w" : [
                    1,
                    2
                ]
            }
        }
    ],
    "stats" : {
        "nscanned" : NumberLong(1),
        "objectsLoaded" : NumberLong(1),
        "avgDistance" : 0,
        "maxDistance" : 0,
        "time" : 2
    },
    "ok" : 1
}
> 

?

?
ÇòÃæµØÀíλÖÃË÷Òý
¸ÅÄÇòÃæµØÀíλÖÃË÷Òý
´´½¨·½Ê½£ºdb.collection.ensureIndex({w:"2dsphere"})
λÖñíʾ·½Ê½£º
GeoJson£ºÃèÊöÒ»¸öµã£¬Ò»ÌõÖ±Ïߣ¬¶à±ßÐεÈÐÎ×´
¸ñʽ£º{type:"",coordinates:[]}
Ö§³Ö$minDistanceÓë$maxDistance
?
´´½¨Ë÷Òý±È½ÏÖØÒªÊôÐÔ½éÉÜ
¸ñʽ
? ? db.collection.ensureIndex({param},{param}) #µÚ¶þ¸ö²ÎÊýÊÇË÷ÒýµÄÊôÐÔ
?
±È½ÏÖØÒªµÄÊôÐÔÓУºÃû×Ö¡¢Î¨Ò»ÐÔ¡¢Ï¡ÊèÐÔ¡¢ÊÇ·ñ¶¨Ê±É¾³ý¡£
?
1. Ãû×Ö£¬nameÖ¸¶¨£ºdb.collection.ensureIndex({},{name:""})
? ? ĬÈÏÃüÃû¸ñʽÊÇÈçÏÂÕâÑùµÄ
? ? µ¥¼üË÷Òý£¬1ºÍ-1£¬ÃüÃûÊǸù¾Ýkey+_1/-1
> db.jerome_2.ensureIndex({x:1})
> db.jerome_2.ensureIndex({y:-1})
> db.jerome_2.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "jerome.jerome_2"
    },
    {
        "v" : 1,
        "key" : {
            "x" : 1
        },
        "name" : "x_1",
        "ns" : "jerome.jerome_2"
    },
    {
        "v" : 1,
        "key" : {
            "y" : -1
        },
        "name" : "y_-1",
        "ns" : "jerome.jerome_2"
    }

?

?
? ? ¸´ºÏË÷Òý
?
> db.jerome_2.ensureIndex({x:1,y:-1})
> db.jerome_2.ensureIndex({x:1,y:-1,z:1})
> db.jerome_2.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "x" : 1,
            "y" : -1
        },
        "name" : "x_1_y_-1",
        "ns" : "jerome.jerome_2"
    },
    {
        "v" : 1,
        "key" : {
            "x" : 1,
            "y" : -1,
            "z" : 1
        },
        "name" : "x_1_y_-1_z_1",
        "ns" : "jerome.jerome_2"
    }
]
mongodb¶ÔË÷ÒýÏÞÖÆÊÇ125×Ö½Ú£¬ËùÒÔÎÒÃÇÐèÒª×Ô¶¨ÒåË÷ÒýÃû×Ö¡£
?
> db.jerome_2.ensureIndex({x:1,y:1,z:1,m:1},{name:"normal_index"})
> db.jerome_2.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "x" : 1,
            "y" : 1,
            "z" : 1,
            "m" : 1
        },
        "name" : "normal_index",
        "ns" : "jerome.jerome_2"
    }
]
> db.jerome_2.dropIndex("normal_index")
{ "nIndexesWas" : 7, "ok" : 1 }
> 

?

?
2. ΨһÐÔ£¬uniqueÖ¸¶¨£ºdb.collection.ensureIndex({},{unique:true/false})
> db.jerome.ensureIndex({m:1,n:1},{unique:true})
{
    "createdCollectionAutomatically" : true,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}
> db.jerome.insert({m:1,n:2})
WriteResult({ "nInserted" : 1 })
> db.jerome.insert({m:1,n:2})
WriteResult({
    "nInserted" : 0,
    "writeError" : {
        "code" : 11000,
        "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: jerome.jerome.$m_1_n_1  dup key: { : 1.0, : 2.0 }"
    }
})
> 

?

3. Ï¡ÊèÐÔ£¬sparseÖ¸¶¨£ºdb.collection.ensureIndex({},{sparse:true/false})
ĬÈÏ´´½¨µÄË÷ÒýÊDz»Ï¡ÊèµÄ¡£
ÒòΪMongoDBûÓй̶¨µÄ¸ñʽ£¬²åÈëµÄʱºò¿ÉÄܲåÈë²»´æÔÚµÄ×ֶΣ¬±ÈÈçx£º1£¬MongoDB»áΪÕâÌõ²»´æÔÚµÄ×ֶδ´½¨Ë÷Òý£¬Èç¹û²»Ï£Íû·¢ÏÖÕâÑùµÄÊÂÇé¿ÉÒÔÖ¸¶¨Ï¡ÊèË÷ÒýΪtrue£¬¾Í²»»áΪ²»´æÔÚµÄ×ֶδ´½¨Ë÷ÒýÁË¡£¿ÉÒÔ¼õÉÙ´ÅÅÌÔÝÓúÍÔö´ó²åÈëËÙ¶È¡£
?
> db.jerome.insert({"m":1})
WriteResult({ "nInserted" : 1 })
> db.jerome.insert({"n":1})
WriteResult({ "nInserted" : 1 })
> db.jerome.find({m:{$exists:true}}) #exists²éÕÒÊý¾Ý¼¯ºÏÖÐÒ»¸ö×ֶδæÔÚ»òÕß²»´æÔڵļǼ
{ "_id" : ObjectId("55729ec1aba41684d6e8826a"), "m" : 1 }
{ "_id" : ObjectId("55729d5caba41684d6e88268"), "m" : 1, "n" : 2 }
> db.jerome.ensureIndex({m:1},{sparse:true}) #´´½¨Ï¡ÊèË÷Òý
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 2,
    "numIndex