Tag: mongoose

mongodb / mongoose在两个数组中find常用元素

假设以下模式: var user = { books : [{bookId : {type: String, ref: 'Book'}}] }; 和以下数据: user1.books[{bookId : 'id1'}, {bookId : 'id2'}] user2.books[{bookId : 'id2'}] finduser1和user2书籍交集的最佳方法是什么? 一个查询会告诉我,user1和user2与bookId:'id2'共有一本书。 谢谢。

mongoose模式中的数字types总是保存为string

我有这个简单的模式,与联系数字数据types。 奇怪的是,input数据(联系人)总是被保存为mongodb中的string。 我不知道背景中发生了什么,或者我错过了什么。 var mongoose = require('mongoose'); var memberSchema = new mongoose.Schema({ . . Other fields are not shown/ necessary . contact: { type: Number, required: true, } }); module.exports = mongoose.model('Member',memberSchema); 在我的路由文件中,我发送用户数据以便像这样添加到数据库中 exports.post = function(req,res,next){ if(!req.body.name || !req.body.email || !req.body.contact) return res.render('index', {error: 'Fill Name, Email and Contact'}); //req.body.contact = parseInt(req.body.contact); var member = […]

Mongoose无法读取具有复杂模式的未定义属性

我有以下用户架构: var mongoose = require('mongoose'); var userSchema = mongoose.Schema({ local: { email: String, password: String }, education: { school: { start: Boolean, name: String, graduationYear: String }, college: { start: Boolean, name: String, career: String, enterYear: String, graduationYear: String } } }); 现在,当我提交包含所有这些信息的表单时,我的应用程序将POST请求路由到以下函数: exports.postEditEducation = function (req, res){ process.nextTick(function (){ var user = req.user; user.education.school.start […]

用Mongoose在Jade中显示数据

我一直在试图抓住存储在mongodb中的数据,并用一个简单的Jade模板来显示它。 我有点新鲜,在这一点上我完全迷失了方向。 这是我在/ yfirlit上渲染集合时的输出 / yfirlit的快速路由器看起来像这样 apiRouter.get('/yfirlit', function(req, res){ apiUser.find(function(err, users) { if(err) res.send(err); res.render('yfirlit', {title: 'Yfirlit', users: users}); }); }); 我简单的玉模板 html head title!= title body div #{users} p | API 当我运行testing时,整个mongodb集合显示在网站上。 我正在寻找的是能够仅显示集合中的一部分文档。 例如:我试图在Jade模板中只显示name属性,但是无法正确显示。 html head title!= title body div #{users.name} p | API 任何帮助将不胜感激,我是如此迷茫,我希望能够渲染出只有我想要的属性,而不是像图片中的整个事情。 干杯!

通过消除具有某个标志的数组对象值来查找集合中的文档

考虑一个包含文档的集合(下面将提到虚拟值): { id: "001", company: "abc", empDet: [ { disableFlag: true, name: 'raj', age: 45, designation: 'clerk' }, { disableFlag: false, name: 'ramya', age: 35, designation: 'manager' }, { disableFlag: false, name: 'gabriel', age: 41, designation: 'asstManager' }, { disableFlag: true, name: 'saran', age: 55, designation: 'attender' }, { disableFlag: true, name: 'buela', age: 25, […]

Mongoose用$或条件更新文档

我想search具有相同的action_target.reply.id或action_target.reply.id所有活动。 像这样的东西: Activity .find({ $or: [ { 'action_object.reply.id': replyId }, { 'action_target.reply.id': replyId } ]}); 但我也只想更新像这样removed属性: Activity .update({ 'action_object.reply.id': replyId }, { 'action_object.reply.removed': true }, { multi: true }); Activity .update({ 'action_target.reply.id': replyId }, { 'action_target.reply.removed': true }, { multi: true }); 有没有可能以某种方式结合这两个查询? 我想更新action_target.reply.removed其中action_target.reply.id或action_target.reply.removed其中action_target.reply.id 。 或者我必须为此编写两个不同的查询,就像我上面所做的那样。

Backbone.save()的ERR_CONNECTION_RESET通过Mongoose更新模型

请告诉我我在做什么错误:我的骨干视图通过mongoose在MongoDB中创build和保存文件,并在视图中使用数据保存()和抓取()方法。 但是当我使用Backbone model.save({option:'modified'}); 与后端的route.put(),jQuery的火灾ERR_CONNECTION_RESET。 我尝试了很多我在网上find的东西,但是没有成功。 也许我需要使用ajax.Prefilter或这种types的东西,但我不知道该怎么做。 骨干视图触发更新中的代码段是: this.user.save({ options: 'modified' }, { wait: true, success: function(model, response) { console.log('saved'); }, error: function(model, error) { console.log(error); } }); 在路由器index.js中 router.put('/user/:id', require('./user').put); 它工作,因为在GET路线,它工作得很好。 在user.js中 exports.put = function(req, res) { var query = { _id: req.params.id }; User.update(query, { options: req.body.options }, function(){ res.send('ok'); }); }; 我也用id或_id做了很多实验,还有各种使用mongoose更新的方法,比如 User.findOne({ […]

find()查询的更短的方法

我想要find提供的坐标单元格是否存在于数据库中。 有没有更简单的方法来创build此查询? var schema = new mongoose.Schema({ coors: { x: String, y: String } }); // coors = {x:'1', y:'2'}; schema.statics.is_cell_exists = function (coors, callback){ var Cell = this; var coors_x = {}; var coors_y = {}; coors_x['coors'] = {x: coors.x}; coors_y['coors'] = {y: coors.y}; var query = { $and: [coors_x, coors_y] }; Cell.find( query […]

用mongoose在不同的对象数组中find一个对象

我必须知道,如果一个项目是在这个模型的第一个或第二个数组中。 Model: _id: String, array1: [{ id: Number, name: String, timestamp: String }], array2: [{ id: Number, name: String, timestamp: String }], array3: [{ id: Number, name: String, timestamp: String }], array4: [{ id: Number, name: String, timestamp: String }] }); 我正在使用这些查询,但任何人都可以帮助我改进它,或者只有一个查询呢? Model.findOne({_id: userId , 'array1.id': item.id },function(error, res) { if(!res){ Model.findOne({_id: userId , 'array2.id': […]

无法使用mongoose保存

借助以下方面的帮助: 1. https://stackoverflow.com/a/30164855/2168120 2. mongodb自动增量 app.js: //some codes var mongoose = require('mongoose'); var connection = mongoose.createConnection("mongodb://localhost/dfp", function(err){ if(err){ console.log(err); }else{ console.log("connected to mongodb from app"); } }); 模式: var userSchema = mongoose.Schema({email: String, fname: String, lname: String, password: String, gender: String, mobile: String, dob:{ type: Date, default: Date.now }, ip: String, doj:{ type: Date, default: […]