向现有MongoDB文档添加字段(在Node.js中使用Mongoose) [英] Add a field to existing MongoDB document (with Mongoose in Node.js)
本文介绍了向现有MongoDB文档添加字段(在Node.js中使用Mongoose)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在MongoDB数据库的 Article
集合中有这个现有文档:
I have this existing document in a collection Article
in MongoDB database:
[ { site: 'www.atlantico.fr',
date: '2014-05-27T11:10:19.000Z',
link: 'http://www.atlantico.fr/example.html',
_id: 538473817eb00f082f4803fc,
__v: 0} ]
我想在Node中使用Mongoose在本文档中添加一个值为'example'
的新字段 day
。 JS。所以我这样做:
I want to add a new field day
with value 'example'
to this document, using Mongoose in Node.js. So I do:
Article.update(
{ link: 'http://www.atlantico.fr/example.html'},
{ $set : {day : 'example'} },
function(err){
});
但它不起作用,因为当我在此之后查询文档时,没有新字段 day
出现...
But it does not work because when I query the document after that, no new field day
appears...
使用更新
或 $ set
,但我找不到我的错误。
I must have made a mistake when using update
or $set
in Mongoose, but I cannot find exactly my mistake.
我错过了什么?谢谢!
推荐答案
试试
Article.update(
{link: 'http://www.atlantico.fr/example.html'},
{day : 'example' },
{multi:true},
function(err, numberAffected){
});
并且不要忘记在架构中添加日期。
and don't forget to add day to schema.
这篇关于向现有MongoDB文档添加字段(在Node.js中使用Mongoose)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文