/* Schema中定义模糊数组,可以添加,不能删除 */
const UserSchema = new Schema({
name:String,
collects:[
{
labels:Array,
_id:Schema.Types.ObjectId,
pic:String,
title:String,
slogo:String,
evaluate:String,
rating:String,
collected:Boolean
}
]
})
1-1 $pull删除数组中符合条件的值
//stores
{id:1000,
cartList:[
{productId:1001,name:'手机'},
{productId:1002,name:"电脑"},
{productId:1003,name:"平板"}
]
}
db.stores.update({id:1000},{$pull:{cartList:{productId:1001}}})
1-2 $push向数组中添加一条数据
db.stores.update({id:1000},{$push:{cartList:{productId:1004,name:"电动车"}}})
1-3 根据某个字段对内嵌数组查询
db.stores.find({'cartList.productId':1001})
UserModel.find({"history._id":id})
User.find({userId,"cartList._id":id})
1-4 只想查询内嵌的数组
db.user.find({},{history:1})
1-5 修改内嵌数组中的某个字段
// 如果记录匹配到多条,用$只进行了第一个元素的更改,需要多条修改那么用$[],如果指定下标修改,那么把$修改为元素的下标即可,索引从0开始
db.user.update({username:'zheng','cartList._id':id},{$set:{'cartList.$.salePrice':3550}})