【问题标题】:How to remove one of the data from output nodejs mongodb database如何从输出nodejs mongodb数据库中删除数据之一
【发布时间】:2021-07-25 13:13:24
【问题描述】:

实际上,我想更改结果中的一些数据,例如添加一行并从返回 json 中删除一行

这里是代码

const Ledgers = db.Ledgers;
const LedgersSync = db.LedgersSync;

      const ledgerList=await Ledgers.find(queryObj)
    
        queryObj["isTransfer"] = 0
        const ledgerListSync=await LedgersSync.find(queryObj)
        
        console.log('ledgerList',ledgerList)
        console.log('ledgerListSync',ledgerListSync)
        
        Array.prototype.push.apply(ledgerList,ledgerListSync);
        return ledgerList 

我得到的结果

{
    "_id": "608bb0725cdd5f0c93d96da0",
    "companyId": 294989,
    "company_id": "60741775b9808d1d885e66d5",
    "Name": "ABC Company",
    "reservedName": "",
    "uid": "4eb25490-8288-4841-a925-ed2512ad6d7b-000000c4",

}

我想要的结果

{
    "_id": "608bb0725cdd5f0c93d96da0",
     "new":test
}

添加新行并删除现有行...

【问题讨论】:

  • 你想从 mongo 中删除一个文档(你称之为“行”)?
  • 文档数据...要编辑
  • 所以你想在 mongodb 中编辑数据?你应该使用 updateOne()
  • 不只是想像我提到的那样显示输出......我只想编辑返回的json
  • 所以搜索“投影”docs.mongodb.com/manual/reference/method/db.collection.find 并请澄清您的问题。

标签: node.js mongodb api postman


【解决方案1】:
  const ledgerList=await Ledgers.find(queryObj)

    queryObj["isTransfer"] = 0
    const ledgerListSync=await LedgersSync.find(queryObj)
    
    console.log('ledgerList',ledgerList)
    console.log('ledgerListSync',ledgerListSync)
    
    Array.prototype.push.apply(ledgerList,ledgerListSync);
    return {"_id":ledgerList ["_id"],  "new":ledgerList["new"]}

只需返回这两个字段!

【讨论】:

  • @Piyush ledgerList 包含什么,
  • 它包含 json 中的 mongodb 集合,例如你可以看到有问题的“我得到的结果”行
  • @Piyush ledgerList 是对象还是数组?
  • ledgerList 是数组
  • 你是如何从该列表中创建 json 对象的,请显示该行代码
猜你喜欢
  • 2017-12-14
  • 1970-01-01
  • 2012-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-03
相关资源
最近更新 更多