單條件查詢:
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}})