Tag: mongoose

mongoosesearch产生神秘的错误

我正在使用Mongoose-Text-Search插件( https://github.com/aheckmann/mongoose-text-search )来search我的mongodb数据库,但是我收到了一个令我困惑的错误消息,我从未见过之前。 error: name=MongoError, ok=0, errmsg=error processing query: ns=testdb.data limit=100 skip=0 Tree: TEXT : query=test, language=, tag=NULL Sort: { $s: { $meta: "textScore" } } Proj: { $s: { $meta: "textScore" } } planner returned error: failed to use text index to satisfy $text query (if text index is compound, are equality predicates given […]

使用Mongoose承诺find并更新

我正在尝试使用Mongoose Promises来获得更清晰的代码(请参阅嵌套函数)。 具体来说,我试图build立这样的事情: Model.findOne({_id: req.params.id, client: req.credentials.clientId}).exec() .then(function(resource){ if (!resource) { throw new restify.ResourceNotFoundError(); } return resource; }) .then(function(resource) { resource.name = req.body.name; return resource.save; <– not correct! }) .then(null, function(err) { //handle errors here }); 所以,在其中的一个承诺,我将需要保存我的模型。 从最新的稳定版本开始,Model.save()不会返回一个promise(bug在这里 )。 要使用经典的保存方法,我可以使用这个: //..before as above .then(function(resource) { resource.name = req.body.name; resource.save(function(err) { if (err) throw new Error(); […]

mongoose – 得到新保存的内部logging的_id

有一个带有子文件的mongoose模式。 我希望能够在父保存之后推送子文档并处理新创build的内部项目。 有没有简单的方法来获得一个新创build的内部项目的_id ? 这里是代码: var trackSchema = mongoose.Schema({ title: String }); var userSchema = mongoose.Schema({ displayName: String, tracks: [trackSchema] }); var Track = mongoose.model('Track', trackSchema); var User = mongoose.model('User', userSchema); var track = new Track({ title: 'Sunrise' }); user.tracks.push(track); // assume 'user' was returned by User.findById() user.save(function (err, userUpdated) { // … is there […]

$ geoWithin查询表单mongodb到mongoose语句

从mongodb获得这个查询: db.location.find( {loc: { $geoWithin: { $centerSphere: [ [ 9, 9], radius ] } }, action:1 }) .limit(10) .skip(1, function(err, result) { console.dir(result); }); 如何将它翻译成使用geoWithinmongoose声明?

如何在Mongoose中填充虚拟内的对象

我已经设置了一个虚拟查询,如下所示: UserSchema .virtual('user_info') .get(function () { return { '_id': this._id, 'username': this.username, 'admin': this.admin, 'email': this.email, 'company': this.company, 'image': this.image, 'profile': this.profile }; }); company领域具有与用户连接的参考公司的标识。 我怎样才能使它填充这个领域,以便我可以得到标题和其他领域Company模型包含?

embedded式文档的Mongoose.js模式选项不起作用

我对node.js和mongoose很新。 我有如下的架构。 var sub = Schema({"title": {type: String, required: true, trim: true, unique: true}, "uniqueId": Schema.ObjectId} var main = Schema({"name": {type: String, required: true, index: { unique: true, dropDups: true}, "subs": [sub]} 这是工作。 文件被插入。 替补正在增加。 问题如下。 我在这里期待的是当我添加新的主入。 它会检查标题是否存在,如果不是,则抛出exception。 它将检查标题是否唯一。 此外,它将自己将对象id分配给uniqueId字段。 但是这没有发生。 这不是检查标题是否存在和唯一。 另外,objectId没有被赋值给uniqueId,而是赋给_id。 这里是将sub添加到main的代码。 MainModel.findByIdAndUpdate( request["_id"], {$push: {subs: {"title": "New title"}}}, {}, function(err, doc) { […]

在单个mongoose命令更新多个文件

我有以下collections [ { a : 'One', b : 'Two' }, { a : 'Three', b : 'Four' } ] 我想在单个查询中更新集合,以便我将子集“编号”附加到集合中每个文档的每个“a”属性。 结果收集应该是: [ { a : 'Onenumberd', b : 'Two' }, { a : 'Threenumbered', b : 'Four' } ] 我怎样才能实现在单mongoose查询相同?

当它不应该时,mongoose发现总是返回真实

我正在使用Mongoose.js来连接我的Mongo数据库。 这个函数search我的Location名称,应该是not found的控制台而不是found ,因为我没有名称!@£ 。 Location.find({name: "!@£"}, function(err, obj) { console.log(obj); if (!err) { console.log("found"); } else { console.log("not found"); } }); 这是注销到我的控制台: [] found 预期的行为应该是它not found控制台日志。 以下是Location模型数据的转储: [ { "_id":"5384c421af3de75252522aa2", "name":"London, UK", "lat":51.508515, "lng":-0.12548719999995228, "__v":0, "modified":"2014-05-27T16:58:09.546Z", "search_count":1 }, { "_id":"5384c766af3de75252522ab4", "name":"Paris, France", "lat":48.856614, "lng":2.3522219000000177, "__v":0, "modified":"2014-05-27T17:12:06.990Z", "search_count":1 }, { "_id":"53851a213a33fe392b758046", "name":"Zagreb, Croatia", "lat":45.8150108, "lng":15.981919000000062, […]

Mongoose API通过_id获取数组中的嵌套项

我正在用Express和Mongoose(前面的Backbone)构buildapi。 我有一个Mongoose用户模型,其中包含一个名为“订单”的数组。 我需要设置一个将通过ID读取单个订单的创build方法。 当我导航到: http://localhost:3000/test/ 我得到以下login用户: { "__v": 0, "_id": "537d09a1fe47a00000c54514", "kittenType": "Grumpy", "local": { "petname": "Smeagol", "password": "$2a$08$X4sF5UmYZ3/2cxfRzpPcq.pphYFRKcb.6xBGupdUyUMgWJlFSr/uq", "email": "julie@gmail.com" }, "orders": [ { "title": "Big Max Fountain", "description": "Large capacity drinking fountain", "quantity": "2", "price": 500, "_id": "53837e9e681808e6ea9f9ca4", "modified": "2014-05-28T23:49:10.232Z" }, { "title": "Lotus Fountain", "description": "Tranquil pools of water", "quantity": "1", "price": […]

mongoose的人口对象正在消失?

我简化了下面的代码: var buySchema = new mongoose.Schema({ userid:String, timestamp:Number, price:Number }); var cardSchema = new mongoose.Schema({ cardid: String, buy:[{type:mongoose.Schema.Types.ObjectId, ref:'buy'}], }); var Card = mongoose.model('cards',cardSchema,'cards'); var Buy = mongoose.model('buy',buySchema,'buy'); Card.findOne({'cardid':cardid}, function(err, card) { var newBuy = new Buy({'userid':userid, timestamp:null,price:null}); newBuy.save(function(err){ card.buy.addToSet(newBuy._id); }); card.save(function(err,card){ console.log(card.buy); // card.buy[] has an ObjectId ref }); }); Card.findOne({'cardid':cardid}, function(err, card) { […]