Tag: mongodb

模型定义之前定义的中间件,级联删除失败,出现中间件被绕过

这是我在我的文件中。 我想要做的是当一个Author文档被删除,他所有的Book文档也应该被删除。 我最初尝试使用连接error handling程序的串行中间件,但没有错误logging, Author被删除,但他的Book没有。 然后我尝试了并行中间件,假设remove()不会被触发,直到所有的前中间件都完成了,但似乎并不是这样。 Author仍被删除,但Book没有,也没有错误logging: //… var Book = require('./book-model''); AuthorSchema.pre('remove', true, function(next, done) { Book.remove({author: this._id}, function(err) { if (err) { console.log(err); done(err); } done(); }); next(); }); AuthorSchema.statics.deleteAuthor = function(authorId, callback) { var Author = mongoose.model('Author'); Author.remove({_id: authorId}, callback); }; // … module.exports = mongoose.model('Author', AuthorSchema); 所以我在考虑中间件被绕过,否则考虑到我尝试过的变化的数量,我会看到至less有一些错误,表明中间件确实被触发。 不幸的是,我似乎无法指出我做错了什么。 请指教。

MongoDB在持久对象之间的原型inheritance

我很好奇,如果MongoDB有select保存一个对象与另一个对象(在同一模式)作为其“原型”。 例如: 假设我们在db中有一个对象,像这样 { name : 'foo', lastName : 'bar', email : 'foo_bar@baz.com' } 我想要做的是得到这个对象,并更新电子邮件(例如) 所以新的对象会变成这样: { name: 'foo', lastName : 'bar', email : 'some_other_email@baz.com' } 正如你可以看到这里有一个重复的数据(即名称和姓氏属性)。 我想要做的只是保存差异,然后对象将在电子邮件道具和原始对象像这样 { email : 'some_other_email@baz.com', __proto__ : originalObjectId //or whatever } 我知道我可以直接更新对象或保存对象的新副本,但我想有不变性,不保存整个对象,我只想保存从原来的差异。 这样的事情是可能的(这是我的意思是mongo支持和优化这样的一个)? (或通过一些像mongoose或类似的东西) 还是我必须自己实施呢? 欢呼声,布吉

卡住实施一个嵌套的数据过滤循环,可能是一个更优雅的方式做到这一点?

这是数据。 我想要做的就是将所有这些数据注入到一个包含所有类别和所有属于这些类别的书籍的variables中: var categories = [ // … { "_id" : "5436b12b456f61180f815c06", "name" : "Horror" }, { "_id" : "5436b43f18bbc8a8073e786e", "name" : "Action" } // … ]; var books = [ // … { "_id" : "5436b43f18bbc8a8073e786d", "title" : "The Bourne Legacy", "category" : "5436b43f18bbc8a8073e786e" }, { "_id" : "5436b5c9de9884e010d5ef24", "title" : "Digital Fortress", "category" […]

如何在mongodb中获取子文档字段的不同值,

我有一个像下面这样的文档的集合。我想要获取属性子文档名称的所有不同的值,它们的值是不同的,并且在集合中计数。 例如: var records = [ { "attributes": [ { "name": "color", "value": "black", "_id": "5441103a0348ebc91ee75b33" } ], "name": "ddd" }, { "attributes": [ { "name": "color", "value": "red", "_id": "5441091393450f1619be99af" }, { "name": "size", "value": "L", "_id": "5441091393450f1619be99b0" } ], "name": "one" }, { "attributes": [ { "name": "color", "value": "black", "_id": "5441092593450f1619be99b1" }, […]

Mongodb,mongoose – dynamic设置TTL时间

我指的是这个时间生活在蒙古,mongoose不工作。 文件不会被删除问我的问题:是否有可能dynamic地设置MongoDB的TTL时间? 所以我们假设你有令牌收集,你想用它来达到不同的目的。 在这种情况下,每次创build令牌时,都会为每个令牌设置特定的TTL。 如果这是可能的,你能否提供一些代码片段?

MongoError:游标已经死了(mongo节点驱动程序)

我正在使用node-mongodb-native 2.0 http://mongodb.github.io/node-mongodb-native/2.0/ 使用以下Node.js代码: var MongoClient = require('mongodb').MongoClient; var mongoUrl = 'mongodb://localhost/twitter'; MongoClient.connect(mongoUrl, function(err, db) { if (err) return console.error(err); var collection = db.collection('tweets'); collection.find().limit(1000).forEach(function(tweet) { console.log(tweet.id); }, function(err) { if (err) console.error(err); db.close(); }); }); 当我设置限制为1000 ( collection.find().limit(1000) )时,我能够检索到前几百条logging,但是我得到了错误消息{ [MongoError: cursor is dead] name: 'MongoError', message: 'cursor is dead' }稍后(我collections了100万条logging)。 但程序运行确定我们指定800作为限制。 没有指定任何限制(只是collection.find() )也是可以的,而脚本只是继续前进而没有任何错误(读取方式多于1000条logging)。 怎么了? […]

在Mongoose插件中添加字段给出“TypeError:架构pathCreatedBy.type”的无效值

我试图做一个CreatedBy Mongoose插件,但是当试图使用ObjectId作为字段types时,它给了我( "account"是另一个定义的集合已经): TypeError: Invalid value for schema path `CreatedBy.type` 这里是插件代码: mongoose = require 'mongoose' module.exports = exports = updatedByPlugin = (schema, options) -> schema.add CreatedBy: type: mongoose.Schema.Types.ObjectId ref: "account" schema.pre "save", (next) -> @CreatedBy = options.accountId next() return schema.path("CreatedBy").index options.index if options and options.index return 那么如何修改ref值使其工作?

如何将集合中的文本文件与fs集合中的图像相关联?

我正在开发meteor的后端,现在我试图用某种方式将文本文件与图像关联起来。 有没有办法在fscollection存储文本文件? 我怎样才能把他们在两个不同的集合? 这是我的两个集合: Images = new FS.Collection("Images", { stores: [new FS.Store.FileSystem("Images", {path: "~/padonde/uploads"})] }); Reynosa = new Mongo.Collection("Reynosa"); 在FsCollection中,我存储图像和其他集合存储数据,但是它们都是同一个logging的一部分。

Mongoose使用'path数组'来填充许多对象{path:objectpath,model:'Model'}

在我的networking应用程序中,订购(SingleOrder)产品后,客户应该检查优惠。 如果可用,那么我应该添加订单到ComboOfferOrder。 在那里,我想检查订单的付款状态。 另外,我必须得到整个产品清单。 我在我的数据库中的所有值在后端。 但是我无法为我的api方法填充“SingleOrder”中的任何对象。 我有下面的模式。 *User* { name : String, email : Sring, role : String } *BankTransaction* { type : String, transationReference: String, date : Date, amount : Number } *ComboOfferOrder* { customer :{ type :Schema.ObjectId, ref : 'User' }, order : { type :Schema.ObjectId, ref : 'SingleOrder' }, productList : [{ […]

parsingJSON nodejs时出错

以下数据是从MongoDB中检索的 console.log(x) 输出 { _id: 54473495721e8a7386959897, tag: 'java', data: [ { view: '2342343', date: '2001/1/25' } ] } 使用JSON.parseparsing var dataJson = JSON.parse(x); 它传递下面的错误 undefined:1 { _id: 54473495721e8a7386959897, ^ SyntaxError: Unexpected token _ at Object.parse (native)