通过猫鼬将项目推入mongo阵列 [英] Push items into mongo array via mongoose

查看:116
本文介绍了通过猫鼬将项目推入mongo阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经琢磨了很多,寻找答案,但我相信我迷失了正确的话来描述我以后的事。



基本上我有一个名为'people'的mongodb集合
该集合的模式如下:

  :{
name:String,
friends:[{firstName:String,lastName:String}]
}

现在,我有一个非常基本的快速应用程序连接到数据库,并成功创建一个空的朋友数组的人。



在应用程序的次要位置,有一个表单来添加朋友。该表单接受firstName和lastName,然后使用名称字段POST以引用适当的人物对象。



我正在努力做的是创建一个新的朋友对象,然后将其推到朋友列表中。



我知道当我通过mongo控制台执行此操作时,我使用更新函数$ push作为我的第二个参数后的查找条件,但我似乎找不到适当的方式来使黑客做这个

  db.people.update({name:John},{$ push:{friends:{firstName: Harry,lastName:Potter}}}); 

更新:
阿德里安的回答是非常有帮助的。以下是为了完成我的目标而做的。



在我的app.js文件中,我使用


$设置了一个临时路由b $ b

  app.get('/ addfriend',users.addFriend); 

在我的users.js文件中我有







$ b var friend = {firstName:req.body.fName,lastName :req.body.lName};
Users.findOneAndUpdate({name:req.user.name},{$ push:{friends:friend}});
};


解决方案

假设 var friend = {firstName:'Harry',lastName:'Potter'};



有两个选项:



更新内存模型,并保存(简单的javascript array.push):

  .friends.push(人); 
person.save(done);

  PersonModel.update(
{_id:person._id},
{$ push:{friends:friend}},
done
);

如果可能,我总是尝试第一个选项,因为它会更多地受益那个猫鼬给你(钩子,验证等)。但是,如果您正在进行大量的并发写入,那么您将会遇到恶劣的版本错误,从而阻止您更换整个模型时间和失去之前你添加的朋友。所以只有当它是绝对必要的时候才去后者。


I've scoured SO a good bit looking for the answer but I'm sure that I'm lost for the right words to describe what I'm after.

Basically I have a mongodb collection called 'people' The schema for that collection is as follows:

people: {
         name: String, 
         friends: [{firstName: String, lastName: String}]
        }

Now, I have a very basic express application that connects to the database and successfully creates 'people' with an empty friends array.

In a secondary place in the application, a form is in place to add friends. The form takes in firstName and lastName and then POSTs with the name field also for reference to the proper people object.

What I'm having a hard time doing is creating a new friend object and then "pushing" it into the friends array.

I know that when I do this via the mongo console I use the update function with $push as my second argument after the lookup criteria, but I can't seem to find the appropriate way to get mongoose to do this.

db.people.update({name: "John"}, {$push: {friends: {firstName: "Harry", lastName: "Potter"}}});

UPDATE: So, Adrian's answer was very helpful. The following is what I did in order to accomplish my goal.

in my app.js file, I set a temporary route using

app.get('/addfriend', users.addFriend);

where in my users.js file I have

exports.addFriend = function (req, res, next)
{
var friend = {"firstName": req.body.fName, "lastName": req.body.lName};
Users.findOneAndUpdate({name: req.user.name}, {$push: {friends: friend}});
};

解决方案

Assuming, var friend = { firstName: 'Harry', lastName: 'Potter' };

There are two options you have:

Update the model in-memory, and save (plain javascript array.push):

person.friends.push(person);
person.save(done);

or

PersonModel.update(
    { _id: person._id }, 
    { $push: { friends: friend } },
    done
);

I always try and go for the first option when possible, because it'll respect more of the benefits that mongoose gives you (hooks, validation, etc.).

However, if you are doing lots of concurrent writes, you will hit race conditions where you'll end up with nasty version errors to stop you from replacing the entire model each time and losing the previous friend you added. So only go to the latter when it's absolutely necessary.

这篇关于通过猫鼬将项目推入mongo阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆