单条件查询:
db.getCollection('task').find({"id": "dc717654-dc45-4bf9-8e8a-1f1b146d6524"})
单条件查询(属性为叶子节点)
db.getCollection('archive_result').find({"results.tags.cluster_id":1263})
结果过滤,存在某个字段:
db.getCollection('表名').find({字段名{$exists:true}})
结果过滤,大于/小于/等于某个值:
db.getCollection('cluster').find({feature_id:{$lt:627808}})
多条件查询,相当于sql语句中的 select * from table where condition A and condition B;
db.getCollection('face_info_01').find({feature_id: 1, target_feature_id: 1})
多个语句的组合使用:
查询并排序,其中1为升序,-1为降序,相当于select * from table order by * desc/asc;
db.getCollection('表名').find({字段名{$exists:true}}).sort({时间:1})
查询并求总数,相当于select count(*) from table where A;
db.getCollection('file_info').find({"file_name":"xidada1.jpg"}).count()
表聚合查询,相当于sql中的join多表查询:
db.file_info.aggregate([
{$match:{"file_name":'xidada1.jpg'}},
{$lookup:
{
from:"task_manager",
localField:"task_id",
foreignField:"id",
as:"xidada_docs"
}
},
{$match:{"xidada_docs.status":'DONE'}}
])
{$match:{"file_name":'xidada1.jpg'}},
{$lookup:
{
from:"task_manager",
localField:"task_id",
foreignField:"id",
as:"xidada_docs"
}
},
{$match:{"xidada_docs.status":'DONE'}}
])
以上语句等同于如下SQL语句:
select * from db.file_info a , db.task_manager b where a.file_name = ''xidada1.jpg' and a.task_id = b.id and a.status = 'DONE' ;
mongodb数据修改
db.getCollection('task').update({"request_id":"989a495a8587479399944b1b638da70f"},{$set:{"status":"ERROR"}})
db.class0.update({name:'阿红'},{$set:{age:24}})
db.class0.update({age:{$gt:20}},{$set:{age:18}},false,true)
db.getCollection('task').update({"request_id":"989a495a8587479399944b1b638da70f"},{$set:{"status":"ERROR"}})
db.class0.update({name:'阿红'},{$set:{age:24}})
db.class0.update({age:{$gt:20}},{$set:{age:18}},false,true)
删除所有:
db.getCollection('cache_item').remove({})
db.getCollection('cache_item').remove({})
条件删除:
db.getCollection('cluster').remove({feature_id:{$lt:627808}})