【问题标题】:How to get the id of the json data inserted in the Mongo db collection with node js app on Bluemix如何使用 Bluemix 上的节点 js 应用程序获取插入到 Mongo db 集合中的 json 数据的 id
【发布时间】:2016-09-27 07:51:27
【问题描述】:

我有一个 json 数据,我想将该 JSON 数据传递给 Mongo db 集合。 json数据是,

json=
{
"customerdetails" : {
    "organId" : "sample",
    "address" : {
        "addressLine1" : "123213",
        "postcode" : "RG16QX"
    },
    "customerName" : "don",
    "dob" : "sample"
},
"transactiondetails" : {
    "bankDetails" : {
        "accountName" : "john",
        "bankAddress" : "sample"
    },
 "brokerId" : "12345"
  }
  }

我只想将 customerdetails 详细信息 json 部分存储在一个集合中。然后我想获取插入的 json 的 id(_id":"nFNBY4GN6m6jjpNEY")..然后我想在客户详细信息值中替换此 id并将这个 json 插入另一个集合中。

{
"customerdetails" :{ "_id":"nFNBY4GN6m6jjpNEY"},

"transactiondetails" : {
"bankDetails" : {
    "accountName" : "john",
    "bankAddress" : "sample"
},
"brokerId" : "12345"
}
}

我使用的代码是

var jsondata=JSON.parse(json);
 var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
 mongourl="*******";
 MongoClient.connect(mongourl, function (err, db) {
      console.log("inside mongo client");

   if (err) {
    console.log('Unable to connect to the mongoDB server. Error:', err);
   } else {
var collection = db.collection('customerdetails');
collection.insert(jsondata.customerDetails, {safe:true},function (err, result) {
  if (err) {
      console.log("got error");
   console.log(err);
  } else {
    var collectionid=result._id;
    console.log("*********"+collectionid);
      console.log("collection"+collection);
   console.log('Inserted documents ");
  }
  });
  }

但我无法插入到收藏中.. 而且,如果它被插入,我如何从集合中获取插入数据的 _id 并将其替换为键值并将剩余的 JSON 传递给另一个集合.. 有人可以提出任何想法。 顺便说一句,我在 bluemix 上使用 nodeapp 和 mongodb 服务。 谢谢

【问题讨论】:

  • 你可以得到它作为 result.id。首先检查控制台中的结果值..并选择id。
  • result.id 就够了
  • 尝试显示或控制你的结果
  • 谢谢..但问题是它没有进入 MongoClient 并且没有打印任何控制台。你看到任何问题 der..我修改了代码

标签: javascript json node.js mongodb ibm-cloud


【解决方案1】:

您的代码中有 2 个问题,我们知道 javascript 区分大小写,您使用的是 jsondata.customerDetails,而您应该使用 json.customerdetails,并且您必须了解 insert 回调函数的参数。

您不需要使用JSON.parse(json);,因为您的变量json 已经是对象。这是代码

var json = {
    "customerdetails" : {
        "organId" : "sample",
        "address" : {
            "addressLine1" : "123213",
            "postcode" : "RG16QX"
        },
        "customerName" : "don",
        "dob" : "sample"
    },
    "transactiondetails" : {
        "bankDetails" : {
            "accountName" : "john",
            "bankAddress" : "sample"
        },
     "brokerId" : "12345"
  }
}

var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var mongourl = 'mongodb://localhost:27017/stackoverflow';

MongoClient.connect(mongourl, function (err, db) {
    console.log("inside mongo client");
    if (err) {
        console.log('Unable to connect to the mongoDB server. Error:', err);
    } else {
        var collection = db.collection('customerdetails');
        collection.insert(json.customerdetails, {safe:true},function (err, result) {
            if (err) {
              console.log("got error");
              console.log(err);
            } else {
              json.customerdetails = {
                  "_id": result.insertedIds[0]
              }
              console.log("********* Here is updated josn ***************");
              console.log(json)
              console.log('Inserted documents ');
            }
        });
    }
});

【讨论】:

  • 谢谢..谢谢..但问题是它没有进入 MongoClient 并且没有打印任何控制台。你看到任何问题 der..我修改了代码..
  • 您可能想复制上面的代码并替换为您的代码。我已经测试过,似乎工作正常。
  • 我修改了我的问题中的代码..它没有进入 MongoClient..如果它进入其中,那么您的插入逻辑将起作用。您可以检查修改后的代码
  • 你还没有按照我的建议修改代码。我已更新我的代码并复制上面的代码并替换为您的代码,然后尝试
  • 是的 arif,,我试过了,但对我来说,这里没有进入 MongoClient
猜你喜欢
  • 2014-07-05
  • 2017-03-15
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 2021-01-16
  • 2018-03-14
  • 2022-11-25
相关资源
最近更新 更多