MongoDB 文檔的查詢和插入操作


MongoDB是文檔型數據庫,有一些專門的術語,和關系型DB相似,但也有差異,例如,Collection類似於關系型DB的Table,document類似於row,key/value pair類似於column。document 是使用{}為邊界,一個Key/Value對使用“:”分割,key/value pair之間使用“,”分割,例如

user={ name:"sue",age:24 }

MongoDB中能夠定義document 數組,這對於批量更新和批量插入操作非常有用。

userArray=
[
  { name:"sue",age:24 },
  { name:"joe",age:25 },
  { name:"pei",age:32 }
]

MongoDB有一個test db,在學習MongoDB時,可以使用use 命令切換到test db。

use test

一,插入操作

MongoDB的插入操作是將document插入到collection中,MongoDB提供三種插入函數db.collection.insert,db.collection.insertOne和db.collection.insertMany,insert函數能夠插入單個doc,也能插入doc的數組。

1,插入單個document

user={ name:"test1", age:22}
db.users.insert(user)
db.users.insert( { name:"test1", age:22} ) db.users.insertOne( {name:"test1", age:22} )

2,批量插入document

user1={ name:"t1", age:21}
user2={ name:"t2", age:22}
user3={ name:"t3", age:23}

db.users.insert([user1,user2,user3])
db.users.insertMany([user1,user2,user3])

--or 
userArray=[user1,user2,user3]
db.users.insertMany([user1,user2,user3])
db.users.insert([user1,user2,user3])

二,查找操作

查詢查詢的語法

db.collection.find( <query filter>, <projection> )

query filter是查詢的過濾條件,projection是從document中查找特定的key/value pair,類似於關系型DB的where子句和select子句。

不加任何query filter時,將查詢所有的document

db.users.find()
db.users.find({})

1,Query filter

1.1 在query filter中,“=”使用 key/value pair,或 $eq 表示,兩者是等價的

{field: <value>}
{ <field>: { $eq: <value> } }

例如,查詢age=21的所有user

db.users.find({age:21})
db.users.find({age:{$eq:21}})

不等式使用$ne表示

{field: {$ne: value} }

例如,查看age<>21的所有user

db.users.find({age:{$ne:21}})

1.2 “>”,“>=”,“<”“<=” 分別使用 $gt,$gte,$lt 和 $lte 表示,格式是

{ field: { $lt: value} }
{ field: { $gt: value} }
{ field: { $lte: value} }
{ field: { $gte: value} }

例如,分別查詢age<22 , age>22,age<=22,age>=22的所有user

db.users.find({age:{$lt:22}})
db.users.find({age:{$gt:22}})
db.users.find({age:{$lte:22}})
db.users.find({age:{$gte:22}})

1.3 邏輯或算符使用 $or 表示,格式是

{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }

例如,查詢age=21或age=22的所有user

db.users.find({$or:[{age:21},{age:22}]})

1.4,邏輯與運算符使用“,” ,或者$and 表示,格式是

{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }

例如,查詢name是“t1”,並且 age=21的所有user

db.users.find({$and:[{name:"t1"},{age:21}]})
db.users.find({name:"t1",age:21})

1.5,在范圍內查詢,使用 $in 表示,不在范圍內,使用$nin表示,格式是

{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }
{ field: { $nin: [<value1>, <value2>, ... <valueN> ] } }

例如,查詢age是21 或 22的所有user,查詢age不是21 和 22的所有user

db.users.find({age:{$in:[21,22]}})
db.users.find({age:{$nin:[21,22]}})

1.6 其他運算符,請參考官方文檔《Query and Projection Operators
2,projection

projection是指定collection中的哪些field需要返回,默認情況下,會返回所有的filed。如果沒有指定"_id"的projection,那么結果集返回"_id",詳細信息參考《Project Fields to Return from Query

a query projection to specifies which fields from the matching documents to return. The projection limits the amount of data that MongoDB returns to the client over the network.

{ field1: <value>, field2: <value> ... }

示例:

db.users.find({$and:[{name:"t1"},{age:21}]},{name:1,_id:0})
db.users.find({$and:[{name:"t1"},{age:21}]},{name:0,_id:0})

3,返回doc 或 iterator

db.collection.find()返回的是cursor,而db.collection.findOne()返回的是single doc。通過db.collection.find() 返回cursor時,必須使用 var 關鍵字定義cursor,如果沒有顯式使用var,那么cursor會自動迭代20次,以顯示前20個doc。

In the mongo shell, when you assign the cursor returned from the find() method to a variable using the var keyword, the cursor does not automatically iterate. However,if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times [1] to print up to the first 20 documents in the results.

step1,使用var 關鍵字定義cursor

var us =db.users.find()

step2,迭代cursor,將doc以json格式顯示

while (us.hasNext())
{
print(tojson(us.next()));
}

step3,使用foreach函數

var us=db.users.find();
us.forEach(printjson);

 

參考文檔:

Query and Projection Operators

MongoDB CRUD Operations

Iterate a Cursor in the mongo Shell


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM