Mongo查询语句
作者:互联网
MongoDB CRUD Operations,官方文档参考:MongoDB CRUD Operations — MongoDB Manual
注意:不同mongodb版本,语法有些许差距,比如:db.{collection}.insertOne等部分版本不支持。如下图示例所示:
1.基本的增删改查操作
1.1 插入文档
# mongo shell操作,选择数据库rongsong
use rongsong
# 给集合fruit插入单条数据
db.fruit.insert({name: "apple"})
# 插入多条数据
db.fruit.insert([ {name: "apple"}, {name: "pear"}, {name: "orange"} ])
1.2 查看文档
(1)查询所有文档语句
db.fruit.find()
# 返回结果
{ "_id" : ObjectId("5e8955c6690ab826e1e07ee8"), "name" : "apple" }
{ "_id" : ObjectId("5e8955c6690ab826e1e07ee9"), "name" : "pear" }
{ "_id" : ObjectId("5e8955c6690ab826e1e07eea"), "name" : "orange" }
(2)过滤查询
db.fruit.find({"name": "apple"})
# 返回结果
{ "_id" : ObjectId("5e8955c6690ab826e1e07ee8"), "name" : "apple" }
(3)正则查询
# 查询文档assert_info字段对应值中包含u'的内容,注意需要转义特殊字符,即:u\'
db.getCollection('http_action').find({"assert_info": {$regex:"u\'"}})
# 接合或操作,"$regex"后面可以接复杂的正则表达式
db.getCollection('job').find({"module": {"$regex": "召回|主题标签"}})
(4)不包含过滤查询
# 过滤status不等于removed的数据
db.getCollection('jobs').find({"status": {"$ne": "removed"}})
(5)大于,小于,大于或等于,小于或等于
db.collection.find({ “field” : { $gt: value } } ); // greater than : field > value
db.collection.find({ “field” : { $lt: value } } ); // less than : field < value
db.collection.find({ “field” : { $gte: value } } ); // greater than or equal to : field >= value
db.collection.find({ “field” : { $lte: value } } ); // less than or equal to : field <= value
(6)in 和 not in ($in $nin)
db.collection.find( { “field” : { $in : array } } );
示例:搜索version为20201101,且assessor为rongsong或wangzhe12的数据
eg: db.getCollection('article_quality_evaluation').find({"version": "20201101", "assessor": {$in: ["rongsong", "wangzhe12"]}})
示例:搜索version为20201101,且assessor不为rongsong或wangzhe12的数据
eg: db.getCollection('article_quality_evaluation').find({"version": "20201101", "assessor": {$nin: ["rongsong", "wangzhe12"]}})
参考:https://blog.csdn.net/u010808135/article/details/89955182?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduend~default-2-89955182.nonecase&utm_term=mongodb%20%E5%AD%97%E7%AC%A6%E4%B8%8D%E7%AD%89%E4%BA%8E
(7)去除重复的值,比如查询满足"version"为"FM_20210106"的所有assessor值(按这个值去重)
distinct
示例:
db.getCollection('recall_evaluation').distinct("assessor",{"version" : "FM_20210106"})
db.getCollection('new_feed_satisfaction_evaluation').distinct("assessor", {"version" : "20210716", "satisfaction": {"$ne": ""}})
(8) 字符串时间范围查询
# 示例
db.getCollection('h5_performance').find({"time": {$gte:"2021-11-11 17:17:35",$lte:"2021-11-11 17:17:38"}})
1.3 删除文档
# 删除单条数据(apple)
db.fruit.remove({"name": "apple"})
# 删除所有数据,一定要谨慎操作
db.fruit.remove({})
1.4 更新文档
(1)单个更新
# 给pear这条数据增加1个字段"address",值为"fujian"
db.fruit.update({"name": "pear"}, {$set:{"address": "fujian"}})
# 将pear这条数据字段"address"的值改为"beijing"
db.fruit.update({"name": "pear"}, {$set:{"address": "beijing"}})
(2)按条件批量更新
# 给满足满足的_id批量更新"manually_tagging"和"manually_tagging_reason"这2个字段
db.getCollection('article_quality_evaluation').updateMany({"_id": {$in: [ObjectId("5faa63723d6865136d019940"), ObjectId("5faa63723d6865136d019941")]}}, {$set:{"manually_tagging": 1, "manually_tagging_reason": "质量高"}})
2.聚合操作
查询语句
db.kg_link.aggregate([{"$group": {"_id": "$@type", "count": {"$sum": 1}}}])
输出结果:
{ "_id" : [ "Team" ], "count" : 324 }
{ "_id" : [ "Knowledge" ], "count" : 62 }
{ "_id" : [ "Product" ], "count" : 276 }
{ "_id" : [ "Person" ], "count" : 33174 }
{ "_id" : [ "Information" ], "count" : 9 }
{ "_id" : [ "Activity" ], "count" : 10 }
{ "_id" : [ "MeetingRoom" ], "count" : 958 }
标签:语句,count,Mongo,db,查询,find,fruit,id,name 来源: https://blog.csdn.net/sinat_33718563/article/details/122189858