Tag: mongoose lodash

如何从数组字段与mongoose获得特定的对象

给定一个像这样的模式: UserSchema = new Schema({ name: String, donations: [ { amount: Number, charity: { type: Schema.Types.ObjectId, ref: 'charity' } } ] }); 我有用户和非营利组织的(string)ID,我想获得用户捐赠的金额。 我用lodash使用这个查询: User.findOne({ _id: userId }, function(err, user) { var amount = _.find(user.donations, {charity: charityId}); console.log("the amount is: ", amount); }); 这里金额返回undefined即使它不应该也猜测我不应该使用lodash 。 给定一个特定的userId和charityId ,获得捐赠金额的正确方法是什么?

我怎样才能更好地优化这些数据库查询/functionstream?

我已经把我的手从游戏开发转移到写支持后端。 到目前为止,一切似乎都奏效了,但是当我写一个朋友系统的时候,我碰到了这个对我来说看起来很脏的functionstream程。 而且我确定我只是在这一点上一起黑客攻击。 任何node.js向导都会告诉我如何改进? 相当肯定我应该在Redis中caching玩家查询。 acceptfriend: function(req, res){ //Find our user User.findById( req.decoded._id, function(err, user){ //error occured if(err){ return res.status(401).send(err); } //no user found if(!user){ return res.status(401).json({ succes: false, message: 'no user found with that id' } ); } //Does the request exist? if( !_.any( user.social.friendRequests, {id: req.params.id} ) ){ return res.status(401).json( { succes: false, […]

从objectId数组中删除一个ObjectId

我有一个objectId是这样的: ["56153e4c2040efa61b4e267f","56033932efefe0d8657bbd9e"]要保存在我的模型中的信息,我使用: items: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Items' }] 我想要做的是拉一个数组的元素,它等于我从​​前端发送的删除请求中的objectId。 我正在使用的代码: _.remove(unit.items, request.params.itemId); 我正在使用lodash库。 我想这个问题是数组有ObjectId元素,我试图比较一个string是request.params.itemId。