Tag: mongoose

我如何创build一个有可选$ in和Mongoose的条件查询?

我正在尝试返回符合a或b或c条件的文档列表。 现在,我只能通过匹配所有条件,而不仅仅是一个工作来实现它的工作。 我试过这个: return User .find() .where({ $or: [ { $in: skillTags }, { $in: roleTags } ]}) 但是我得到一个错误。 这个工作,但不是我想要的,因为它只返回匹配skillTag和roleTag的结果。 我想要至less与其中一个或另一个匹配的文档: return User .find() .where({ skillTags: { $in: skillTags } }, { roleTags: { $in: roleTags } })

像mongoose文件的结构一样在树上recursion

我需要帮助从我的数据库通过get请求拉出对象的对象,所以我可以在我的网页上显示所有的对象,但是我运行的这个testing已经失败了,debugging它,它只是运行到永远,并且从不接收JSON。 我认为我的逻辑可能会被搞乱。 我不确定是否应该使用asynchronous,这看起来像callback地狱,现在想想我应该,但我不知道如果在优化思考如果这是一个体面的方式,我通常从最坏的情况下,我试图低于我的目标是围绕O(N),因为有N个文件,我只是遍历一个recursion树,还有一些额外的查询,以find最初的文件和什么不是这样,N + X(其中x是额外的查询量<N)。 但是我觉得我好像在思想上错过了一些东西,好像有更好的办法。 我有一个架构,像这样 Doc { title : string, author : string, subDoc : [{type: mongoose.Schema.Types.ObjectId, ref : 'Doc'}] } 所以有我生成的文件,每次我生成这些文件我有这些文件可以存在的子文档,甚至这些子文档可以有子文档。 然后我们结束了一些树状结构。 但是我不想显示文档>然后它的sub_doc1>所有的子目录…等>其子文档2>所有的子文档..>等等。 不过,我不知道我在想什么是有效的,甚至是值得做这么多的查询。 app.get('/docs/:doc/subDocTree', function(req, res) { var array = []; var id = req.params.doc; var num_proc = 0; console.log(id); Doc.findById(id, function(err, doc) { console.log(doc); for (var x = 0; x […]

达到收集结束时,可放大光标closures

我正在尝试从MongoDB上限集合中传输一些数据,如下所示: var query = {}; var options = {tailable: true, awaitdata: true, numberOfRetries: Number.MAX_VALUE}; var stream = myColl.find(query, options).stream(); stream.on('data', function(doc){ console.log(doc); }).on('error', function (error){ console.log(error); }).on('close', function () { console.log('closed'); }); 如预期的那样打印文档,但是在到达集合结束时,streamclosures。 是否有可能防止这种情况发生? 我希望程序打印数据到达集合中,无限期地等待集合的最后获取更多数据。

mongoose引用不起作用

我创build了两个Schema,User和Whisp(一个whisp就像一个tweet)。 //User var UserSchema = new mongoose.Schema({ username: { type: String, required: true, index: { unique: true } }, password: { type: String, required: true } }); UserModel = mongoose.model("UserSchema", UserSchema); module.exports.User = UserModel; //Whisp var WhispSchema = new mongoose.Schema({ text : String, created_at : Date, created_by : {type : Schema.Types.ObjectId, ref : "UserSchema"}//i want […]

使用Mongoose引用具有未知模型types的对象

使用Mongoose时 ,有可能有一个字段引用另一个对象,当该文档的模型/types是未知的? 例如,我有模型:照片,评论,提交,post等,我想有一个Like模型,指向他们: var Like = new Mongoose.Schema({ // What would the value of `ref` be, should it just be left out? target: { type: Schema.Types.ObjectId, ref: '*' } }); 根据我的理解, ref需要成为一个模型。 我可以把它全部放在一起,但是我仍然可以从这种方式得到Mongoose 填充方法的好处吗?

Find()函数retunrs undefined – Mongoose和Node.js

我试图用Node.js和Mongoose做一个简单的函数,如果模型是空的,返回true。 mongooseconfiguration是好的: var mongoose = require('mongoose'); var db = mongoose.createConnection( 'mongodb://localhost:27017/prueba' ); var userSchema = mongoose.Schema({ phoneNumber: Number, name: String }); var User = db.model('User', userSchema, 'User''); 然后我试图做到这一点: User.find(function(err, data) { if (err) {console.log(err)}; console.log(data.length == 0 ); }); 它工作正常,它logging真实,或错误。 然后我试着做: var isUsersEmpty = function () { User.find(function(err, data) { if (err) {console.log(err)}; console.log(data.length == […]

用自定义的UID设置mongo的_id

背景: 我无法控制来自我的数据库文档“ 事情”的事件 。 当post进来时,我写了一个名为findOrCreate()的函数(它会在Things文档中find或创build一个logging(object?))。 为了减less冗余,我想使用第三方POST负载字段nuid来replace该logging(对象)的默认_id 。 题: 我如何正确地replace_id与有效载荷字段nuid ? 示例负载: curl -H "Content-Type: application/json" -X POST -d '{"participant":{"nuid":"98ASDF988SDF89SDF89989SDF9898"}}' http://localhost:9000/api/things 架构 'use strict'; var mongoose = require('mongoose'), Schema = mongoose.Schema; var ThingSchema = new Schema( {nuid: String} ); module.exports = mongoose.model('Thing', ThingSchema); thing.controller.js //*currently this function will have both _id and a nuid (which works […]

mongoosesorting填充字段

以及我有以下shemas: var BrandSchema = new Schema({ 名称: { 键入:String, 要求:真, 索引:{ 独特:真实 }, 小写:真 }, 商标: { 键入:ObjectId, ref:'Image' } }); var FollowActionSchema = new Schema({ 'actionDate':{ 键入:date, 'default':Date.now }, '牌': { 键入:ObjectId, ref:'品牌' }, “表演者”:{ 键入:ObjectId, ref:'User' }, 'type':string//跟随用户,跟随品牌,跟随买家 }); 我想要的是让用户跟随品牌,按品牌名称sorting,所以为了做到这一点,我做了FollowAction的查询,并find用户所做的所有FollowActions,然后填充品牌字段。 所以问题是我不能sorting查询的品牌名称,唯一的方法,我知道这样做是通过返回所有文件,并从nodejs应用程序sorting。 任何人都知道我该怎么做? 或者如果我应该改变shema结构? 我做的查询是: async.waterfall([ 函数findActions(next){ var options = { 查询:{ 执行者:userId, $或:[{ […]

TypeError:无法读取Mongoose 4.0.3中未定义的属性“path”

我正在尝试向mongoosescheme添加一些validation器。 我的模型看起来像: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var Usr = new Schema({ _id: { type: String }, email1: { type: String }, password: { type: String }, admin: { type: Boolean }, firstName: { type: String }, lastName: { type: String }, hasCar: { type: Boolean }, phone: { type: Number } […]

从mongo获取不同的坐标

这是我的Mongo模型: Schema = new mongoose.Schema({ userId: {type: mongoose.Schema.Types.ObjectId, required: true, index: true, ref: 'user'}, date: {type: Number, index: true}, coords: {type: [Number], index: '2dsphere', required: true} //not distinct } 我试图按以下格式按date检索不同的坐标: [[long, lat],[long, lat],[long, lat], …, [long, lat]] 我写了这样的东西: module.exports = function(req, res, next) { var days = req.params.days || 365; db.model('activity').aggregate([ {$match: {date: {$gte: getDatesFromNumDays(days)[0], […]