//查询商品名称长度大于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"]
}
}
}
}
}
]);