这是我的架构设计
mongoose.Schema({
traffic_countries:String,
company_details:String,
campaigns:[{
_campaignid : mongoose.Schema.Types.ObjectId,
}],
impressions:[{
country:String,
_campaignid :mongoose.Schema.Types.ObjectId
}],
payments:[],
budget:String })
我想做什么
我想将对象推送到Payments数组下面,我为此目的使用的代码在对象下面给出
{
"id" : "PAY-52L98986JB4797714LN73QTY",
"intent" : "sale",
"state" : "created",
"payer" : {
"payment_method" : "paypal"
},
"transactions" : [
{
"amount" : {
"total" : "12.00",
"currency" : "USD"
},
"description" : "",
},
"related_resources" : [ ]
}
],
"create_time" : "2018-08-24T07:48:30Z",
"links" : [
{
"rel" : "execute",
"method" : "POST"
}
],
"httpStatusCode" : 201
}
我为此目的使用的代码
上述对象存储在我从贝宝(Paypal)api获取的交易变量中,并推送到付款数组中
var newPayment = {
$push:{
payments:{
transaction
}
}
}
Advertiser.updateOne({_id:req.user.id},newPayment,function(err,rec){
if(err) throw err;
})
存储在mongodb中的对象就像
payments:[
{ transaction:{object} },
{ transaction:{object} },
{ transaction:{object} }
]
但是我想存储
之类的对象payments:[
{object},
{object},
{object}
]
答案 0 :(得分:0)
经过大量时间浪费后,我自己找到了解决此问题的方法,我只需要删除推送查询中的多余括号即可
代替此
var newPayment = {
$push:{
payments:{
transaction
}
}
}
我需要
var newPayment = {
$push:{
payments:transaction
}
}