程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MongoDB數據庫 >> MongoDB綜合知識 >> MongoDB快速入門筆記(四)之MongoDB查詢文檔操作實例代碼

MongoDB快速入門筆記(四)之MongoDB查詢文檔操作實例代碼

編輯:MongoDB綜合知識

MongoDB簡介

MongoDB 是一個基於分布式文件存儲的數據庫。由 C++ 語言編寫。旨在為 WEB 應用提供可擴展的高性能數據存儲解決方案。

MongoDB 是一個介於關系數據庫和非關系數據庫之間的產品,是非關系數據庫當中功能最豐富,最像關系數據庫的。

下面給大家介紹MongoDB查詢文檔操作的實例

先把student刪除,再重新插入數據

> db.student.drop()
true
> db.student.insert([{ "_id" : 1, "name" : "zhangsan", "age": 27, "sex": 1 }, { "_id" : 2, "name" : "lisi", "age": 27 } ,{ "_id" : 3, "name" : "wangwu", "age": 30 }, { "_id" : 4, "name" : "zhaoliu", "age": 28 }, { "_id" : 5, "name" : "qianliu", "age": 33 }, { "_id" : 6, "name" : "sunba", "age": 32 }])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 6,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
> db.student.find()
{ "_id" : 1, "name" : "zhangsan", "age" : 27 }
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 6, "name" : "sunba", "age" : 32 } 

1、查詢指定鍵

db.集合名稱.find({查詢條件},{指定鍵})

指定鍵:1表示顯示,0表示不顯示,_id默認顯示

> db.student.find({},{name:1})
{ "_id" : 1, "name" : "zhangsan" }
{ "_id" : 2, "name" : "lisi" }
{ "_id" : 3, "name" : "wangwu" }
{ "_id" : 4, "name" : "zhaoliu" }
{ "_id" : 5, "name" : "qianliu" }
{ "_id" : 6, "name" : "sunba" }
> db.student.find({},{_id:0, age:0})
{ "name" : "zhangsan", "sex" : 1 }
{ "name" : "lisi" }
{ "name" : "wangwu" }
{ "name" : "zhaoliu" }
{ "name" : "qianliu" }
{ "name" : "sunba" }
> db.student.find({},{_id:0, name:1})
{ "name" : "zhangsan" }
{ "name" : "lisi" }
{ "name" : "wangwu" }
{ "name" : "zhaoliu" }
{ "name" : "qianliu" }
{ "name" : "sunba" } 

2、各種查詢方式

$lt:<
$lte:<=
$gt:>
$gte:>=
$ne:!=
> db.student.find({age:{$lt:30}})
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
> db.student.find({age:{$ne:27}})
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 6, "name" : "sunba", "age" : 32 } 

$in:包含

$nin:不包含

> db.student.find({age:{$in:[27,28]}})
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
> db.student.find({age:{$nin:[27,28]}})
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 6, "name" : "sunba", "age" : 32 } 

$or:或者

> db.student.find({$or:[{age:{$lt:29}}, {name:"sunba"}]})
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 6, "name" : "sunba", "age" : 32 } 

null:空值

> db.student.find({sex: null})
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 6, "name" : "sunba", "age" : 32 } 

$type:鍵是某種類型的

double:1
string:2
...

> db.student.insert({_id:7, name:7, age:70})
WriteResult({ "nInserted" : 1 })
> db.student.find({name: {$type: 2}})
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 6, "name" : "sunba", "age" : 32 }
> db.student.find({name: {$type: 1}})
{ "_id" : 7, "name" : 7, "age" : 70 } 

正則表達式

> db.student.find({name: /si\b/})
{ "_id" : 2, "name" : "lisi", "age" : 27 } 

db.集合名稱.findOne({查詢條件},{指定鍵})
查詢出符合條件的第一條數據

> db.student.findOne()
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 } 

db.集合名稱.find({查詢條件},{指定鍵}).limit(數字)
查詢前幾條數據

> db.student.find().limit(3)
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 3, "name" : "wangwu", "age" : 30 } 

db.集合名稱.find({查詢條件},{指定鍵}).skip(數字)
跳過前幾條數據

> db.student.find().skip(2)
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 6, "name" : "sunba", "age" : 32 }
{ "_id" : 7, "name" : 7, "age" : 70 } 

可以使用limit()和skip()實現分頁

> db.student.find().skip(0).limit(3)
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
> db.student.find().skip(3).limit(3)
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 6, "name" : "sunba", "age" : 32 }
> db.student.find().skip(6).limit(3)
{ "_id" : 7, "name" : 7, "age" : 70 } 

db.集合名稱.find().sort({鍵:數字})
數字為1表示升序,數字為2表示降序

> db.student.find().sort({age:1})
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 6, "name" : "sunba", "age" : 32 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 7, "name" : 7, "age" : 70 }
> db.student.find().sort({age:1, _id:-1})
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 6, "name" : "sunba", "age" : 32 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 7, "name" : 7, "age" : 70 } 

以上內容是小編給大家介紹的MongoDB快速入門筆記(四)之MongoDB查詢文檔操作實例代碼,希望對大家有所幫助!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved