在mongodb的test庫:
我們擁有十載網(wǎng)頁設(shè)計和網(wǎng)站建設(shè)經(jīng)驗,從網(wǎng)站策劃到網(wǎng)站制作,我們的網(wǎng)頁設(shè)計師為您提供的解決方案。為企業(yè)提供網(wǎng)站設(shè)計制作、網(wǎng)站設(shè)計、微信開發(fā)、小程序定制開發(fā)、手機網(wǎng)站開發(fā)、H5開發(fā)、等業(yè)務(wù)。無論您有什么樣的網(wǎng)站設(shè)計或者設(shè)計方案要求,我們都將富于創(chuàng)造性的提供專業(yè)設(shè)計服務(wù)并滿足您的需求。> db.data.insert({name:"1616",info:{url:"http://www.1616.net/",city:"beijing"}});
> db.data.insert({name:"hao123",info:{url:"http://www.hao123.com/",city:"beijing"}});
> db.data.insert({name:"ll4la",info:{url:"http://www.114la.com/",city:"dongguan"}});
2.對字段 info 創(chuàng)建索引:
> db.data.ensureIndex({info: 1});
3.data表的索引查詢:
rs0:PRIMARY> db.data.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.data"
},
{
"v" : 1,
"key" : {
"info" : 1
},
"name" : "info_1",
"ns" : "test.data"
}
]
4.索引的用法:
以下查詢是可以用到info的索引的:
>db.data.find({info: {url:"http://www.1616.net/", city:"beijing"}});
>db.data.find({info: {url:"http://www.1616.net/"} });
>db.data.find({info: {city:"beijing"});
可以使用query.explain()查看索引的使用:
rs0:PRIMARY> db.data.find({info: {city:"beijing"}}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.data",
"indexFilterSet" : false,
"parsedQuery" : {
"info" : {
"$eq" : {
"city" : "beijing"
}
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"info" : 1
},
"indexName" : "info_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"info" : [
"[{ city: \"beijing\" }, { city: \"beijing\" }]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "mycentos.WORKGROUP",
"port" : 27017,
"version" : "3.2.8",
"gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0"
},
"ok" : 1
}
但是這樣的查詢就不行:
>db.data.find({"info.city":"beijing"}); //字段部分必須加引號
>db.data.find({info.url:"..."});
這樣的查詢語句,只能使用類似的組合索引:
> db.data.ensureIndex({"info.url":1, "info.city":1});
5.組合索引
> db.data.ensureIndex({"info.url":1, "info.city":1});
即使查詢時,與定義的排序相反,也是可以使用索引掃描的。
rs0:PRIMARY> db.data.find({"info.url": /http:*/i}).sort({"info.url": -1, "info.city":-1}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.data",
"indexFilterSet" : false,
"parsedQuery" : {
"info.url" : /http:*/i
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"filter" : {
"info.url" : /http:*/i
},
"keyPattern" : {
"info.url" : 1,
"info.city" : 1
},
"indexName" : "info.url_1_info.city_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "backward",
"indexBounds" : {
"info.url" : [
"[/http:*/i, /http:*/i]",
"({}, \"\"]"
],
"info.city" : [
"[MaxKey, MinKey]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "mycentos.WORKGROUP",
"port" : 27017,
"version" : "3.2.8",
"gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0"
},
"ok" : 1
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。