Tag: mongoose

Mongooseasynchronous调用另一个模型使validation不可能

我有两个mongoose模式,看起来像这样: var FlowsSchema = new Schema({ name: {type: String, required: true}, description: {type: String}, active: {type: Boolean, default: false}, product: {type: Schema.ObjectId, ref: 'ClientProduct'}, type: {type: Schema.ObjectId, ref: 'ConversionType'}, }); 然后将这个模式embedded到如下所示的父模式中: var ClientCampaignSchema = new Schema({ name: {type: String, required: true}, description: {type: String}, active: {type: Boolean, default: false}, activeFrom: {type: Date}, activeUntil: {type: Date}, […]

Mongo查询和响应“GroupBy”实现使用Mongoose和/或underscore.js

我们有以下格式的mongo收集messages … /* 0 */ { "text" : "A message to John", "created" : ISODate("2015-01-29T23:50:10.488Z"), "to" : { "id" : ObjectId("54bf1dc6c65b030c00faec0d"), "name" : "John Smith" }, "from" : { "id" : ObjectId("54bf1e1ceb5bf8800b0f5a8c"), "name" : "Martin Fowler" }, } /* 1 */ { "text" : "Another message to John", "created" : ISODate("2015-01-30T00:37:38.106Z"), "to" : { "id" […]

在MongoDB中使用Mongoose + Node.js存储非结构化的JavaScript对象

在我的node.js应用程序中,我需要在MongoDB中存储非结构化的JavaScript对象。 我在Mongoose中指定了以下模型: module.exports = mongoose.model('DBAllocation', { from: Date, expires: Date, userSession: Object, allocationTimestamp: Date, allocationPriority: Number, vmGroupID: String, allocationRequestContent: Object }); 通过指定userSession和allocationRequestContent的数据types为Objecttypes,我想将JavaScript对象(不指定其结构)保存到MongoDB中并按原样检索它。 但是,当我将模型保存到数据库中时,出现内部错误。 我试图存储以下项目: var allocation = new Allocation({ _id: allocationID, from: Date.now(), expires: null, userSession: authorizedRequest.session, allocationTimestamp: Date.now(), allocationPriority: <some number>, vmGroupID: <some number>, allocationRequestContent: authorizedRequest.requestContent }); authorizedRequest.session和authorizedRequest.requestContent是两个JavaScript对象。 但是当我用{}replace它们时,模型会成功保存。 我听说过我们可以用来存储非结构化数据的strict参数,但是我怀疑是否可以用它来实现我所需要的。 有没有办法来实现这个呢? 任何帮助将非常感激。 更新: 我发现authorizedRequest.session是一个MongoDB模型,我用authorizedRequest.session.toObject()replace了它,并用一个简单的对象{'cat':'123','dog':'456'replaceauthorizedRequest.requestContent […]

如何更新从Mongoose中的多个数组的数组内的值?

我有一个像下面的User mongodb结构。 { "_id" : ObjectId("54d2f68edbe2337d000e1568"), "username" : "khayusaki@gmail.com", "organization" : [ { "idOrganization" : "54c8b01af927db992d021f58", "usertype" : 1, "displayname" : "K", }, { "idOrganization" : "54c8b01af927db992d021f60", "usertype" : 2, "displayname" : "Khay", } ], "__v" : 0 } 这意味着像用户可以有许多组织,并且每个显示名称和usertype都依赖于它。 我想要做的是,我只想从请求主体使用JSON来更新一个组织的显示名称。 { "Displayname" : "Khay Usaki", "Email" : "khayusaki@gmail.com" } 我的mongoose查询如下所示。 User.findOne({username: req.body.Email}, {organization: {$elemMatch: […]

mongoose:三路文件join

我正在学习mongoose,需要一些帮助。 我有3个集合,并且在一个API调用中,我想创build3个互相引用的文档; “join”如下: 用户 – 需要引用chirps video – 需要引用chirps 啁啾声 – 需要引用用户和唧唧声 问题:我知道我可以做一个model.create(),并在每个callback中传递新文档,然后更新到相应的文档,但我想知道是否有一个更干净的方法来做到这一点? 对不起,如果我不清楚的问题。 请问我是否有意义。 码 var chirpSchema = new mongoose.Schema({ date_created: { type: Date, default: Date.now } , content: { post : String } , _video: { type: $oid, ref: "video" } , _author: { type: $oid, ref: "user" } }); var chirp = […]

mongoose路由特定查询输出

mongoose新问题。 我试图构build一个MEAN应用程序,根据用户input的参数查询现有的数据库。 数据库正在由外部程序填充JSON。 到目前为止,当我使用下面的路由器时,我有我的路由器成功地显示所有的Mongologging: // Pulls all JSONs router.get('/jsons', function(req, res, next) { Json.find(function(err, jsons){ if(err) { return next(err);} res.json(jsons); }); }); 现在我试图创build一个单独的路线,可以显示该数据库中的特定字段。 (其中TestLocation =“纽约”) // Pulls a JSON with City New York router.get('/jsons/NewYork', function(req, res, next) { var queryNYC = Json.where({TestLocation: "New York"}); queryNYC.findOne(function(err, jsons) { if(err) { return next(err);} res.json(jsons); }); }); 尽pipe原始路由表明一个JSONlogging的确具有纽约的TestLocation,但是这对我来说是空的。 […]

MongoDB / Mongoose有很多关系

所以我试图找出在Mongo / Mongoose的人口和关系。 我有一个将有许多预测的夹具。 如果我想显示预测的夹具,我可以简单地使用填充方法,但是如果我需要显示夹具的所有预测呢? 这听起来很简单,但也许我仍然有我的SQL头试图弄清楚。 这是容易做还是我以错误的方式接近? 这是我的模特 – var FixtureSchema = new Schema({ homeTeam: { type: Number, required: true, ref: 'Team' }, awayTeam: { type: Number, required: true, ref: 'Team' }, date: { type: Date }, matchday: { type: Number, required: true } }); var PredictionSchema = new Schema({ goalsHomeTeam: { type: Number, required: […]

mongoose – 插入两个集合:一个是ID依赖于另一个

我有一个数组我想在我的数据库中插入: tracks = [new Track({name : "myname1"}), new Track({name : "myname2"})] Track.collection.insert(tracks, {}, function(err, tracks){ console.log('err', err); } 但即使数组中有2个对象,insert()方法也会抛出一个错误: [RangeError: Maximum call stack size exceeded] (“track”doc是创build的,但没有任何内容。) 编辑 Mongoose – RangeError:最大调用堆栈大小超过解决第一个问题,但没有解决我真正的问题: 当你从模型创build一个对象时,你可以得到_id,我用它来创build另一个对象。 但是不可能插入一个Mongoose对象的集合,因为解决的问题是: 你只能使用一个数组JSON对象 。 data.items.forEach(function(item,index){ tracks.push(new Track({name : "myname1"}) ); posts.push(new Post({trackId : track._id}) ); } Track.collection.insert(tracks, {}, function(err, tracks){ console.log('err', err); }

如何在Mongoose中embedded文档中的字段

覆盖模型。 var CoverageSchema = new Schema({ module : String, source: String, namespaces: [{ name: String, types: [{ name: String, functions: [{ name: String, coveredBlocks: Number, notCoveredBlocks: Number }] }] }] }); 我需要在各个层面上进行覆盖块集合: *Module: {moduleBlocksCovered}, // SUM(blocksCovered) GROUP BY module, source **Namespaces: [{nsBlocksCovered}] // SUM(blocksCovered) GROUP BY module, source, ns ****Types: [{typeBlocksCovered}] // SUM(blocksCovered) BY module, […]

抛出Mongoose pre中间件的自定义错误,并使用蓝鸟承诺

我用蓝鸟承诺使用mongoose。 我正在尝试在validation前中间件中抛出一个自定义错误,并使其捕捉到Bluebird catch。 这是我的validation方法 schema.pre('validate', function(next) { var self = this; if (self.isNew) { if (self.isModified('email')) { // Check if email address on new User is a duplicate checkForDuplicate(self, next); } } }); function checkForDuplicate(model, cb) { User.where({email: model.email}).count(function(err, count) { if (err) return cb(err); // If one is found, throw an error if (count […]