查询
where 查询:
db.getCollection("xxxx").find({"id":NumberLong('11111111111')}).limit(10)
查询表行数
db.getCollection("xxxx").count()
查询某age和sex列,过滤条件,并且把sex列的列名改成性别:
db.getCollection("xxxx").aggregate(
[
{
"$match": {
"sex": "男",
"category": {
"$in": ["数学", "语文"]
},
'fieldName': {exists: true, $ne: null },
'createTime': {
$gte: ISODate('2022-07-01T00:00:00Z'),
$lte: ISODate('2022-08-01T00:00:00Z')
},
}
},
{ "$project": {
"_id": 0,
"age": 1,
"性别": "$sex",
"生日":{$dateToString:{format:"%Y-%m-%d",date:"$birth"}},
"number": "$number",
"时间": ""
}},
//查询前10条
{ $limit: 10 },
//如果想分组,就写下面这个group
,{
"$group": {
"_id": "$colname",//分组字段
"number": {
$avg: "$age"
}
}
}
]);
新增
单个修改
批量修改
db.getCollection("table").update({"aaa":"0108"},{$set:{"bbb":"360"}},{multi: true});
删除
创建视图
db.createView("view_name", "collection_name", [
{
$match: {
abc: "2109245374"
}
},
{
$project: {
_id: 1,
abc: 1,
def: 1
}
}
])