Mongodb 常见操作:查询,排序,查看索引,创建索引等
//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.json
db.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}})
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2022-09-25 IOC控制反转,DI依赖注入,自定义IOC及生命周期,反射
2022-09-25 .NET 性能调优,检测工具
2022-09-25 jenkins+gitee,jenkins+gitlab+harbor
2022-09-25 kubernets 1.25 版本
2008-09-25 sql数据类型 控制语句 高级数据过滤 字符串查询 多表查询