Tag: mongoose

填充另一个对象引用的对象

我有一个产品列表,每个产品都有自己的选项。 例如: 蓝色连衣裙(S – L) 红色连衣裙(XS – S – M) 蓝色礼服和红色礼服是产品, S , L , XS , S和M是select。 选项模型有一个产品模型的参考,我想检索所有的产品,然后列出自己的选项。 我只想用一个查询来实现它,我的问题是我从没有链接到它的选项的产品开始。 所以我开始find所有的产品,并与嵌套, 然后与foreach循环,我得到了所有的select。 然后我尝试将选项分配给产品对象(在我的情况下, forEach中的productElem ),但是当我将其从范围当中取出时,它是空的。 如何填充从产品查询开始的选项? 产品架构: var schema = new Schema({ imagePath: {type: String}, title: {type: String, required: true}, description: {type: String, required: true} }); 选项架构: var productOptionSchema = new Schema({ type: {type: String, […]

无法使用mongoose连接到MongoDb。 MongoDB安装在我的本地系统中

我不能使用mongoose连接到MongoDb。 我已经在我的本地系统中安装了MongoDB //Import the mongoose module var mongoose = require('mongoose'); //Set up default mongoose connection`enter code here` var mongoDB = 'mongodb://localhost/my_database'; mongoose.connect(mongoDB, { useMongoClient: true }); //Get the default connection var db = mongoose.connection; //Bind connection to error event (to get notification of connection errors) db.on('error', console.error.bind(console, 'MongoDB connection error:')); module.exports = mongoose.connection; 得到错误: // […]

mongoose不返回所有的领域

所以我有: UserSchema : var UserSchema = new mongoose.Schema({ firstName: String, lastName: String, image: { type: String, default: "http://via.placeholder.com/250×200" }, email: { type: String, required: [true, "Email missing."], unique: true }, kind: { type: String, enum: ["Admin", "Teacher", "Student"], default: "Student" }, isAdmin: { type: Boolean, default: false }, isPromote: { type: Boolean, default: false }, […]

npm start命令不起作用,并显示mongoose的弃用函数错误

我正在创build一个express和有angular项目。 我会告诉你的步骤,所以你可以看到,如果问题是一个缺less的命令或否: 我开始在全球安装express: npm install -g express 然后我创build了我的快速项目: express –view=hbs project_name 我安装了依赖关系: npm install 我安装了所有angular4需要的依赖关系: npm install –save @angular/cli@latest npm install –save @angular/core @angular/common @angular/http @angular/router @angular/compiler @angular/compiler-cli @angular/platform-browser @angular/animations @angular/platform-server … 我安装了mongodb和mongoose: npm install –save mongoose 现在,我试图运行命令npm start ,并且出现以下错误: 0信息它的工作,如果它结束与确定1详细cli ['C:\ Program Files \ nodejs \ node.exe',1详细cli 'C:\ Users \ pc \ AppData \ […]

mongoose寻找function,search不返回所需的结果

我有一个Mongodb中的用户列表,需要根据如下图所示的filter进行search: 只有性别是强制性的,用户可能有或没有其他细节 User.find({ "gender": userEntry.gender, "dob": { $gte: convert.getDobFromAge(userEntry.ageHigherLimit), $lte: convert.getDobFromAge(userEntry.ageLowerLimit), $exist: false }, "details.chest": { $gte: userEntry.chestLowerLimit, $lte: userEntry.chestHigherLimit, $exist: false }, "details.waist": { $gte: userEntry.waistLowerLimit, $lte: userEntry.waistHigherLimit, $exist: false }, "details.height": { $gte: userEntry.heightLowerLimit, $lte: userEntry.heightHigherLimit, $exist: false }, "details.weight": { $gte: userEntry.weightLowerLimit, $lte: userEntry.weightHigherLimit, $exist: false } }, function (err, users) { […]

用mongoose,express和node.jssearch表单 错误:发送后无法设置标题

我试图做一个search使用作为参考从Monitoria集合的字段,我正在使用自己的路线和作为方法GET的forms,但我没有得到它,我得到这个错误,这个消息路线: 错误信息 消息在路线中。 “Pesquisar”是用葡萄牙语search的 代码路线:(“Pesquisar”,它是在葡萄牙search) app.route('monitorias/pesquisar') .get(function(req, res) { //var monitoria = req.body.nomeMonitoria; //console.log(monitoria); Monitoria.find({name: req.body.nomeMonitoria}, function(err, monitoria) { if (err) { res.json(err); } else { //res.render('result', {"monitoria": monitoria[0]} ); console.log(monitoria); } }); }); 在html文件中的表单:(“Pesquisar”是用葡萄牙语search的) <form action="/monitorias/pesquisar" method="get" class="form-inline"> <label class="sr-only" for="nomeMonitoria">Monitoria</label> <input type="text" class="form-control" id="nomeMonitoria" name="nomeMonitoria" placeholder=" Name of Monitoria…"> <button type="submit" class="btn btn-primary">Search</button> […]

节点的Js – 我需要ref或embedded我的Mongoose架构?

我知道在mongoose schema有很多关于ref和embed模式的讨论。 我有点学习node ,我有一个RDBMs的背景。 我正在和mongoose一起工作。 我有一个非常简单的场景,我已经实现了它。 我卡住的地方是要么使用embed或ref 。 脚本 我的情况是,我有两个模型product和categories 。 它们之间的关系是many to many 。 首先让我告诉你我的模型。 类别架构 const mongoose = require('mongoose'); //function to call monggose schema const CategorySchema = mongoose.Schema({ category_name: { type: String, required: true } }) const Category = module.exports = mongoose.model('Category', CategorySchema); 产品架构 const mongoose = require('mongoose'); const ProductSchema = mongoose.Schema({ product_name: […]

CastError:投射到ObjectId失败的值“types:”缓冲区“”

试图创build一个新的用户在我的Web应用程序没有意识到已经有一个用户在我的数据库用户名相同。 出于某种原因,这个边界案例打破了我的应用程序,现在每次我尝试去主页,我得到: (节点:14649)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):CastError:强制转换为ObjectId失败的值为“{type:'Buffer',data:[89,243,30,231,66,69,45, (节点:14649)[DEP0018]弃用警告:不处理的承诺拒绝已被弃用。 将来,未处理的承诺拒绝将使用非零退出代码来终止Node.js进程。 是什么使这个问题独特的从这里的其他问题在stackoverflow是事实,types是'缓冲区'。 没有其他问题有这个具体问题。 我不知道如何创build一个缓冲区,当我没有在我的数据库或节点服务器中使用它们。 这是在缓冲区错误之前发生的重复错误消息: {MongoError:E11000重复键错误集合:crud2.users索引:username_1 dup键:{:“lightbeam”}在Function.MongoError.create(/ Users / * /桌面/编码/应用程序/ thequoteapp / node_modules / mongodb核心/在/ Users / * / Desktop / coding / lib目录下的toError(/Users/*/Desktop/coding/apps/thequoteapp/node_modules/mongodb/lib/utils.js:139:22)中的lib / error.js:31:11) apps / thequoteapp / node_modules / mongodb / lib / collection.js:668:23 at handleCallback(/Users/*/Desktop/coding/apps/thequoteapp/node_modules/mongodb/lib/utils.js:120:56)at / Users /*/Desktop/coding/apps/thequoteapp/node_modules/mongodb/lib/bulk/unordered.js:465:9 at handleCallback(/ Users / * / Desktop / […]

保存方法不工作在mongoose

我在node.js中使用下面的代码: async function create(data) { utils.removeIdFields(data); const user = new User(data); console.log("user will be printed here", user); await user.save(); console.log("code not reaching here"); return user; } 在上面的代码中,User是一个mongoose模型,在user.save()之上的行一直工作正常。 但是,它从不打印下一个日志。 我正在使用mongoose版本4.11.0。

mongoosefindOne不工作如何我想要它

我正在制作一个应用程序,用户logging他们睡觉的小时数,他们的饮食习惯等,这些信息被存储在一个名为Journal的集合中。 当用户点击“统计信息”时,它会触发一个名为find()的函数,它查找用户提交的所有日志条目,并进行控制台日志logging。 这是我迄今为止: exports.stats = function(req, res) { function find() { Journal.findOne({'user': req.params.id}, 'sleep', function(err, journal){ if(err) throw err; console.log(journal.sleep); }); } find(); } 在这个例子中,我正在试图控制一个特定用户的睡眠小时数,但是它只显示该会话的睡眠小时数,而不是所有睡眠小时数