四:查询
1: 查询business集合内latitude大于30,longitude小于50,state位于AZ的10条记录
查询business集合内city为"Charlotte"或"Toronto"或“Scottsdale”的记录(跳过前510条数据)
db.business.find({ latitude: { "$gte": 30, "$lte": 50 }, state: "AZ" }).limit(10)
result:
查询business集合内city为"Charlotte"或"Toronto"或“Scottsdale”的记录(跳过前510条数据)
db.business.find({ city: { "$in": ["Charlotte", "Toronto", "cottsdale"] } }).skip(150)
result :
五索引:
创建索引:friend数据集上,建立user_id(升序)与friend_id(降序)多字段唯一索引
db.friend.createIndex({user_id:1 ,friend_id: -1})
result
查看索引:
db.friend.getIndexes()
六聚合:
统计review数据集中stars大于2.0对应的不同user_id(作为_id)的stars评分总和(重命名为starSum)
db.review.aggregate([ { $match: { "stars": { "$gte": 2.0 } } }, { $group: { _id: "$user_id", starSum:{ $sum: "$stars" } } }, ])
result :
统计friend数据集中friend_id为"BI4jBJVto2tEQ0NiaR0rNQ"的不同用户的总数(count)从第10条开始统计
db.friend.aggregate([ { $match: { friend_id:"BI4jBJVto2tEQ0NiaR0rNQ" } }, { $group: { _id: "$friend_id", Sum:{ $sum: "$count", } } }, ]).skip(10)
result :
统计friend数据集中不同的friend_id(distinct)
db.friend.distinct( "friend_id" )
result :
总结
到此这篇关于mongodb数据库实验之增删查改的文章就介绍到这了,更多相关mongodb增删查改 内容请搜索