Tag: coffeescript mongoose

在一个大的Mongo集合中,如何使用mongoose检索唯一的ID(或任何其他特定的属性)?

假设我有一个Person集合和一个Car集合,并且所有的人都有一辆车,Person文档存储了这个车的Id。 一路上,由于后端validation不好,一些汽车一直坚持数据库,没有任何业主。 我想删除所有这些'孤儿'车。 我的做法可能不是最优化的,但是这里有: exports.cleanOrphanCars = (req, res) -> Car.find() #find all the cars .exec(err, cars) -> return res.send(StatusMessages.ERROR.code, { error: err }) if err? carIds = cars.map (car) => #map the IDs to a new array for faster iteration return car._id Person.find() .exec(err, people) -> peoplesCarIds = (person) => return person.carId for carId in […]

在node-csv-parser中调用node-mongodb-native:RangeError:超出最大调用堆栈大小

我使用node-csv-parser读取csv数据,并使用mongoose将它存储在mongodb中。 但是我试图加快导入,并且我想使用node-mongodb-native公开的本地保存方法进行评估,使用Model.collection以mongoose访问。 (这是我在Mongo总部的办公时间与一位蒙古工程师的build议)。 每次读入csv的新行时, node-csv-parser触发一个data事件。 在这个事件里面,我读了一行数据,从中创build一个新的数据点,并保存在mongo中。 我可以使用我的mongoose模型TestDataPoint在data事件内保存数据点。 但是,如果我尝试改为创build一个javascript对象的数据点,并保存使用TestDataPoint.collection.save ,我得到的错误: RangeError: Maximum call stack size exceeded 。 我已经尝试过以各种不同的方式调用本地保存,包括通过mongoose.connection.db.collection("testdatapoints")直接获取集合,并将其发送到由asynchronous模块提供的队列,但始终或多或less的相同的结果。 我可以在我的代码中的其他地方使用本地驱动程序成功保存数据点,甚至在csv导入的end事件中,也可以不在data事件中。 我也通过logging来确定,在我当前的设置(64位AMD处理器上的Ubuntu 12.04,8 GB RAM)上,代码在抛出堆栈错误之前迭代了154行csv,但没有数据写入数据库从这个data事件里面。 似乎无意中发生了某种recursion(?),或者也许是node-csv-parser和node-mongodb-native之间的某种错误。 为了澄清,我的(编辑/更新)示例代码如下,日志重复154次: about to call native save just called native save 然后说: in native save callback for dataPoint: 1 Native save failed, error:RangeError: Maximum call stack size exceeded in native save callback for […]

Mongodb / Mongoose查询

User有一堆Essays ,作为一个ObjectIds数组存储。 在CoffeeScript中,模式是: User = new Schema essays: [Schema.Types.ObjectId] name: String Essay = new Schema grade: Number text: String author: Schema.Types.ObjectId 如何在一个查询中获得最近一篇文章写在最后一天, 最近一篇文章的评分在80到90之间的所有不同用户? 我有这个(在CoffeeScript中): req.mongoose.Essay .find( _id: $gt: # trick which creates an ObjectId with yesterday's timestamp mongoose.Types.ObjectId.createFromHexString( (~~(yesterday.getTime() / 1000)).toString(16) + "0000000000000000" ) ) .where("grade").gt(80).lt(90) .popluate(path: "User") .exec (err, docs) -> console.log ({name: essay.author.name} […]

用Mongoose查询嵌套的embedded式文档

我试图查询内嵌的embedded式文档。 我试图“填充”结果,但是失败了。 如何找回查找电话中的所有书籍详情? 我想要一个用户书架上的所有书籍对象,我可以从中获取数据? ### Trying to query nested embedded documents using Mongoose. Database Outline for example An Owner has multiple bookshelves which each have an array of books. A book is not unique, and the same book could be on many different shelves. ### mongoose = require("mongoose") Schema = mongoose.Schema mongoose.connect "localhost", "d1" bookSchema […]

Mongoose save()方法不写入数据库

所以我正在用mongoose更新用户子文档的一个属性,但它并没有保存到我的数据库中。 这是我的function: @User.findOne({'email': email}, (err, user) -> if err? callback(err) else if user? for account in user['accounts'] if account['account_uuid'] is account_uuid account.state = "Verified" user.save((err, updated_user, numberTouched) -> if err? console.log err return callback(err) else console.log 'Successfully verified account' console.log updated_user return callback(null, 'Successfully verified account') ) return callback(new Error('No account for account_uuid')) ) 真正令人沮丧的是, […]

如何使用mongoosefindOne

我有下面的模式(道歉,它是在咖啡脚本) Schema = mongoose.Schema AuthS = new Schema auth: {type: String, unique: true} nick: String time: Date Auth = mongoose.model 'Auth', AuthS 我只想恢复一个绝对在我的数据库中的logging: Auth.findOne({nick: 'noname'}, function(obj) { console.log(obj); }); 不幸的是这总是logging为null 。 mongo shell中的db.auths.findOne({nick: 'noname'})总是返回一个值。 到底是怎么回事?