gpt4 book ai didi

MongoDB $从 ObjectID 数组中提取值

转载 作者:可可西里 更新时间:2023-11-01 10:31:57 41 4
gpt4 key购买 nike

我收藏了这份文件:

{
"_id" : ObjectId("52718433e18a711923000005"),
"owners" : [
ObjectId("52718433e18a711923000004"),
ObjectId("52ed40dccc5bc50000000003"),
ObjectId("52ed4171abe2780000000003")
]
}

我有以下声明,其中我试图删除 owners 字段中的值之一:

Business.update({_id:req.body._id}, {$pull:{"owners":req.body.userid}}, function(err){
if(err){
res.json(500, {message:"Could not remove user from admin list"});
}else{
res.json(200);
}
});

我知道 req.body._idreq.body.userid 具有有效值:

{ _id: '52718433e18a711923000005',
userid: '52ed4171abe2780000000003' }

其他的操作,比如通过ID找商家等都是可以的,所以不是ObjectId格式的问题。还能是什么?

--编辑:这是我的(缩写的)模式定义:

var BusinessSchema = new Schema({
business_name: {type: String, required: true},
owners: {type: Array}
});

最佳答案

您当前的架构未向 Mongoose 提供有关 owners 数组字段中包含的数据类型的任何指示。如果您希望 Mongoose 将您的字符串转换为 ObjectID,您需要在您的架构中提供类型信息:

var BusinessSchema = new Schema({
business_name: {type: String, required: true},
owners: [{type: Schema.ObjectId}]
});

关于MongoDB $从 ObjectID 数组中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21563020/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com