简体   繁体   English

无法将阵列添加到 mongodb

[英]Can't add array to mongodb

I'm trying to send an array to mongodb, but the res.json(user) returns an empty biddingGroup:[] and mongodb document never has field biddingGroup appear.我正在尝试向 mongodb 发送一个数组,但是res.json(user)返回一个空的biddingGroup:[]并且 mongodb 文档永远不会出现字段 bidingGroup。 I've looked at stack posts and have seen suggestions for schema.我查看了堆栈帖子并看到了有关架构的建议。 I've tried我试过了

biddingGroup: [{type: String}],
biddingGroup: [String],
biddingGroup: {type: String},

I haven't found a working schema that captures the data yet.我还没有找到捕获数据的工作模式。

I even hardcoded biddingGroup: ['test'] too, but it never shows up.我什至也硬编码了 bidGroup: ['test'] ,但它从未出现过。

app.js应用程序.js

app.put('/api/listings/:id', (req, res) =>

Post.update({
  id: req.query.id
}, {
  $set: {
    currentBid: req.query.currentBid,
    lastBidTimeStamp: req.params.lastBidTimeStamp,
    biddingGroup: ['test']
  }
}, {
  multi: false //set to false to ensure only one document gets updated
}).exec().then(data => {
  console.log(data);
}, err => {
  console.log(err);
})
    );

Any help is appreciated.任何帮助表示赞赏。

You need to use exec() at the end to run the query.您需要在最后使用exec()来运行查询。 That is the function that actually runs the request and returns you the promise.那就是实际运行请求并返回 promise 的 function。 Plus your usage of the update function in general is off.另外,您对update function 的使用通常已关闭。

Try this:尝试这个:

Post.update({
  id: req.query.id
}, {
  $set: {
    currentBid: req.query.currentBid,
    lastBidTimeStamp: req.params.lastBidTimeStamp,
    biddingGroup: ['test']
  }
}, {
  multi: false //set to false to ensure only one document gets updated
}).exec().then(data => {
  console.log(data);
}, err => {
  console.log(err);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM