OS:Windows10
MongoDB:MongoDB-3.4.4
网盘链接:https://pan.baidu.com/s/1tPUBNjlwqSdhCS89LpS6pQ
提取码:cwhk
可视化界面:mongodb-compass-1.28.4-win32-x64
网盘链接:https://pan.baidu.com/s/1GZA1Y66GnhtitF2OLzFkig
提取码:jz89
(以上软件均可从MongoDB官网免费下载使用)
每个文档对应订单中某个商品相关信息,包括:
db.items_wjw.insert({gnumber:"P003",quantity:2,price:5})
db.items_wjw.insert({gnumber:"P002",quantity:2,price:8})
db.items_wjw.insert({gnumber:"P002",quantity:1,price:4})
db.items_wjw.insert({gnumber:"P001",quantity:10,price:4})
db.items_wjw.insert({gnumber:"P003",quantity:4,price:10})
db.items_wjw.insert({gnumber:"P001",quantity:10,price:20})
db.items_wjw.insert({gnumber:"P003",quantity:10,price:20})
db.items_wjw.insert({gnumber:"P002",quantity:5,price:10})
db.items_wjw.find().pretty()
var cursor = db.items_wjw.find() //定义游标
print(cursor) //打印游标变量
print(cursor.size()) //输出集合中文档的个数
while(cursor.hasNext()){
printjson(cursor.next()) //使用printjson输出结果数据集
}
注:db.collection.createIndex(keys, options) //语法中 Key 值为你要创建的索引字段,1 为指定按升序(逆序)创建索引,如果你想按降序(正序)来创建索引指定为 -1 即可。
db.items_wjw.createIndex({gnumber:-1})
查看集合索引
db.items_wjw.getIndexes()
db.items_wjw.createIndex({gnumber:1,price:-1})
查看集合索引
db.items_wjw.getIndexes()
db.items_wjw.find({price:{$gt:5}}).pretty()
db.items_wjw.find({quantity:5,price:{$gte:5}}).pretty()
db.items_wjw.find({$or:[{quantity:10},{price:{$lte:10}}]}).pretty()
db.items_wjw.find({$or:[{pnumber:"P003",quantity:10},{price:{$gte:5}}]}).pretty()
注:在查询语句后加上explain()方法即可查看在此查询中所用到的索引情况。
db.items_wjw.find({$or:[{pnumber:"P003",quantity:10},{price:{$gte:5}}]}).explain()
总结:本文介绍了MongoDB数据库中的文档查询操作!
之前写的MongoDB数据库的有关博客:
后续会继续更新有关MongoDB数据库的内容!
(注:第22次发文,如有错误和疑问,欢迎在评论区指出!)
——2021.11.29