mongodb一些常用的查询语句

//查询商品名称长度大于25个字符的商品

db.my_collection.find({item_name:{$exists:true},$where:"(this.item_name.length > 25)"}).limit(5);

//查询商品名称长度小于5个字符的商品

db.my_collection.find({$where:"this.item_name.length < 5"}).limit(5);

//查询指定ObjectId的数据

db.my_collection.find({'_id':ObjectId('63328fe79a0e8c5bd8f5b094')});

//更新指定ObjectId的数据

db.my_collection.update({
    "_id": ObjectId("6358884fe8809ba5f7604ce2")
}, {
    $set: {
        "publish_time": "2022-10-26 07:47:00"
    }
}, {
    multi: false
});

//删除指定ObjectId的数据

db.my_collection.remove({'_id':ObjectId('6333dfe79a0e8cefccac185c')});

//时间戳转日期

db.my_collection.aggregate(
   [
     {
       $project: {

          date1Str: {$dateToString: {format: "%Y-%m-%d %H:%M:%S:%L", date:{"$add":[new Date(0),"$timestamp"]}}},
          date2Str: {$dateToString: {format: "%Y-%m-%d %H:%M:%S:%L", date:{"$add":[new Date(0),"$timestamp",28800000]}}}
       }
     }
   ]
)

字段转ObjectId

db.my_collection.aggregate([
    {
        "$project": {
            "my_collection_id": {
                "$convert": {
                    "input": "$my_collection_id",
                    "to": "objectId"
                }
            }
        }
    }]);

// 时间戳转日期字符串

db.getCollection("my_collection").aggregate([
    {
        $project: {
            date: {
                $dateToString: {
                    format: "%Y-%m-%d",
                    date: {
                        $add: [new Date(0), "$publish_timestamp"]
                    }
                }
            }
        }
    }
]);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值