1、根据ID主键删除一条数据
db.user_main.remove({_id:ObjectId("60f9618b07043d180")});
2、根据ID主键修改一条数据的 platformId 字段
db.user_main.update({_id : ObjectId("6113c8d38822c62c")},{$set:{platformId:"68203"}});
3、根据查询条件,然后按照 platformCode 分组(如果不需要分组,则_id:null), total_fee 求和
db.user_main.aggregate([
{
$match:{
trade_time:{$gte:ISODate("2021-06-01T00:00:14.560Z"),$lte:ISODate("2021-06-31T23:59:00Z")}
}
},
{
$group: {
_id: "platformCode",
totala:{$sum:"$total_fee"}
}
}
]);
4、根据查询条件,然后按照 platformCode 分组(如果不需要分组,则_id:null), refund_fee 求和(refund_fee为字符串,需要转成double)
db.user_main.aggregate([
{
$match:{
trade_time:{$gte:ISODate("2021-06-01T00:00:14.560Z"),$lte:ISODate("2021-06-31T23:59:00Z")}
}
},
{
$group: {
_id: "platformCode",
totala:{
$sum:{$toDouble:"$refund_fee"}
}
}
}
]);
5、根据mainId列段分组查询,查询统计结果大于1的
db.user_main.aggregate([
{
$group: {
_id: "$mainId",
count:{$sum:1}
}
},
{
$match:{
"count":{"$gt":1}
}
}
]);
6、多表关联,用主表ID关联子表的mainId。user_main表Id为ObjectId类型,改成String,关联user_platform表的mainId
db.user_main.aggregate([
{
"$project": {
"id": {
"$convert": {
"input": "$_id",
"to": "string"
}
}
}
},
{
"$lookup": {
"from": "user_platform",
"localField": "id",
"foreignField": "mainId",
"as": "member"
}
}
]);
7、多表关联(查询platformId、firstTime字段),用主表ID关联子表的mainId。主表Id为ObjectId类型,改成String,关联user_platform表的mainId
db.user_main.aggregate([
{
"$project": {
"id": {
"$convert": {
"input": "$_id",
"to": "string"
}
}
}
},
{
"$lookup": {
"from": "user_platform",
"localField": "id",
"foreignField": "mainId",
"as": "member"
}
},
{
"$project":{
"_id":false,
"userId":"$member.platformId",
"createTime":"$member.firstTime"
}
}
]);
第一次编辑,希望能给广大小伙伴带来些许的帮助。不完善之处,还请小伙伴们指出,后续慢慢完善,谢谢!