未使用mongodb和node.js从其他集合获取id
我在特定的未使用mongodb和node.js从其他集合获取id,node.js,mongodb,Node.js,Mongodb,我在特定的userId下添加产品,但没有从其他集合中获取userId api.js 插入数据库后,必须从数据库中请求用户。 e、 g.根据您的代码,以下内容应适用: var insertDocument = function(db, callback) { // Based on your code we assume that you have only one user in the database. Otherwise you have to do a find for a sp
userId
下添加产品,但没有从其他集合中获取userId
api.js
插入数据库后,必须从数据库中请求用户。 e、 g.根据您的代码,以下内容应适用:
var insertDocument = function(db, callback) {
// Based on your code we assume that you have only one user in the database. Otherwise you have to do a find for a specific user.
db.collection('users').findOne(function (err, user) {
if (err) { return callback(err); }
db.collection('proInfo').insertOne( {
"Product_Name": json.Product_Name,
"Brand": json.Brand,
"Color": json.Color,
"Image": json.Image,
"Price": json.Price,
"Rating": json.Rating,
"Description": json.Description,
"Category": json.Category,
"Url": urla,
"userId": user._id
}, function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the proInfo collection.");
// Below I am passing the error as a first param to your callback. You can implemented as you wish, but it is a convention to use error as a first param.
callback(err, result);
});
});
};
插入数据库后,必须从数据库中请求用户。 e、 g.根据您的代码,以下内容应适用:
var insertDocument = function(db, callback) {
// Based on your code we assume that you have only one user in the database. Otherwise you have to do a find for a specific user.
db.collection('users').findOne(function (err, user) {
if (err) { return callback(err); }
db.collection('proInfo').insertOne( {
"Product_Name": json.Product_Name,
"Brand": json.Brand,
"Color": json.Color,
"Image": json.Image,
"Price": json.Price,
"Rating": json.Rating,
"Description": json.Description,
"Category": json.Category,
"Url": urla,
"userId": user._id
}, function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the proInfo collection.");
// Below I am passing the error as a first param to your callback. You can implemented as you wish, but it is a convention to use error as a first param.
callback(err, result);
});
});
};
您如何将
userId
传递到mongodb中?“userId”:db.collection('users').findOne({“name”:“admin”})。\u id,像这样我正在传递我认为MongoClient
在第一个查询中不支持另一个查询。它插入json
文档。尝试先从数据库获取用户,然后传递“userId”:“1”
如何将userId
传递到mongodb?“userId”:db.collection('users')。findOne({“name”:“admin”})。\u id,像这样我正在传递我认为MongoClient
在第一次查询中不支持另一个查询。它插入json
文档。尝试先从数据库中获取用户,然后传递“userId”:“1”
好的,我知道了。那么我如何将用户id传递给“proInfo”集合。@HemanthkumarHJ我编辑了我的答案,所以你首先获取你的用户(根据你的代码,你只有一个,因此你没有findOne),然后将id传递给你的insert查询。还有一件事是搜索用户id及其产品。我知道了。那么我如何将用户id传递给“proInfo”集合。@HemanthkumarHJ我编辑了我的答案,所以您首先获取您的用户(根据您的代码,您只有一个,因此您没有findOne),然后将id传递给您的insert查询。还有一件事是搜索用户id及其产品