//insert

db.books.insert(

[

{ _id: 12, item: "pencil1", qty: 51, type: "no.1" },

{ _id: 21, item: "pencil1", qty: 52, type: "no.2" },

{ _id: 31, item: "pencil1", qty: 53, type: "no.3" },

]

)db.books.insert([

{ 3 "title" : "book-51", 4 "type" : "technology", 5 "favCount" : 11, 6 "tag":[],

"author" : {

"name" : "fox",

"age" : 28

}

},{

"title" : "book-52",

"type" : "technology",

"favCount" : 15,

"author" : {

"name" : "fox",

"age" :

}

},{

"title" : "book-53",

"type" : "technology",

"tag" : [

"nosql",

"document"

],

"favCount" : 20,

"author" : {

"name" : "fox",

"age" : 28

}

}])//$project 投影别名 聚合操作

db.books.find()

db.books.aggregate([{$project:{name:"$item",_id:0,type2:1}}])//$group

db.books.aggregate([

{$group:{_id:"$item",total:{$sum: 1}}},

{$sort:{total: -1}}

])//$match

db.books.aggregate([{$match:{"item":"pencil1"}}])//$count

db.books.aggregate([

{$match:{type:"pencil1"}},

{$count: "qty"}])//$ join

db.customer.aggregate([

{$lookup:{

from: "order",

localField: "customerCode",

foreignField: "customerCode",

as: "customerOrder"

}

}])db.order.aggregate([

{$lookup:{

from: "customer",

localField: "customerCode",

foreignField: "customerCode",

as: "customer"

}

},

{$lookup:{

from: "orderItem",

localField: "orderId",

foreignField: "orderId",

as: "orderItem"

}

}

])db.customer.insert({customerCode:1,name:"customer1",phone:"13112345678",address:"test1"

})db.customer.insert({customerCode:2,name:"customer2",phone:"13112345679",address:"test2"

})

db.order.insert({orderId:1,orderCode:"order001",customerCode:1,price:200})

db.order.insert({orderId:2,orderCode:"order002",customerCode:2,price:400})

db.orderItem.insert({itemId:1,productName:"apples",qutity:2,orderId:1})

db.orderItem.insert({itemId:2,productName:"oranges",qutity:2,orderId:1})

db.orderItem.insert({itemId:3,productName:"mangoes",qutity:2,orderId:1})

db.orderItem.insert({itemId:4,productName:"apples",qutity:2,orderId:2})

db.orderItem.insert({itemId:5,productName:"oranges",qutity:2,orderId:2})

db.orderItem.insert({itemId:6,productName:"mangoes",qutity:2,orderId:2})$sort

db.books.find().sort({qty:-1})

db.books.find().sort({qty:-1}).explain()$使用mongoimport工具导入数据

mongoimport -h 192.168.65.1 -d test -u user -p pwd --authenticationDatabase=admin -c

zips --file D:\ProgramData\mongodb\import\zips.jsondb.books.aggregate([{$match:{item:"pencil1"}}]).explain()

db.books.find({$match:{item:"pencil1"}}).explain()$createIndex()

db.books.createIndex({item:1})

db.books.find({item:"pencil1"}).explain()db.books.createIndex({type:1,item:1})

db.books.find({qty:{$gt:51}})