如何使用JS将空数组保存到mongodb中 [英] How can I save an empty array into mongodb using js
问题描述
基本上,我的应用程序正在运行,但我遇到了一个问题:如果我传递的对象包含要保存的空数组,则该数组不会保存到数据库中。我不确定这是js还是mongo驱动程序中的问题,但是为了保存空数组,我需要像这样传递数组: products:['']
。
Basically I got my app up an running but I'm stuck with a problem: if I pass an object that contains an empty array to be saved, the array is not saved into the db. I'm not sure this is a problem in js or the mongo driver, but in order to save the empty array I need to pass the array like so: products: ['']
.
这是我的mongo文档的结构:
This is the structure of my mongo document:
_id: ObjectId(...),
name: 'String',
subcategories: [
{
subcategory: 'string',
products: [
{
name: 'string'
price: integer
}
]
}
]
因此在我的前端,我通过ajax调用抓取整个文档,将一个新对象推入子类别数组。新对象看起来像这样:
So in my front-end I'm grabbing the whole document through an ajax call pushing a new object into the subcategories array. The new object looks like this:
{子类别:'字符串',产品:['']}
这行之有效,直到我需要在数组中插入一个新对象:因为我已经抓住了整个对象,所以将新对象推到了数组中,前一个看起来像这样:
And this works okay until I need to insert a new object inside the array: Because I've grabbed the whole object, pushed the new object to the array, the previous one looks like this:
{子类别:'string'}
在此过程中没有提及 products:[]
数组。
Having lost the mention to products:[]
array in the process.
我如何获得围绕这个?我需要在我的对象中有一个空数组。
How can I get around this? I need to be able to have empty arrays in my object.
编辑
我在前端执行的操作:使用 $。get
获得了整个对象,该对象返回:
What I did on front end: Got the whole object with $.get
which returned:
var obj =
_id: ObjectId(...),
name: 'String',
subcategories: [
{
subcategory: 'Subcategory1',
products: [
{
name: 'string'
price: integer
}
]
}
];
然后在前端,我将新对象类别推送到子类别数组中:
Then on the front end I've pushed the new object category inside the subcategories array:
data.subcategories.push({subcategory: 'Subcategory2', products: ['']})
其中subcat是具有类别名称的字符串。在我的数据库上,我可以看到已经成功添加了对象:
Where subcat was a string with the category name. On my db I could see that I've successfully added the object:
var obj =
_id: ObjectId(...),
name: 'String',
subcategories: [
{
subcategory: 'Subcategory1',
products: [
{
name: 'string'
price: integer
}
]
},
{
subcategory: 'Subcategory2'
products: []
}
];
问题是当我想添加另一个子类别时,前一个返回空:
The problem was when I wanted to add another subcategory, the previous one return empty:
var obj =
_id: ObjectId(...),
name: 'String',
subcategories: [
{
subcategory: 'Subcategory1',
products: [
{
name: 'string'
price: integer
}
]
},
{
subcategory: 'Subcategory2'
},
{
subcategory: 'Subcategory3'
products: []
},
];
因为在某个时候,空数组已从对象中删除。就像我说的,我确实在前端修复了此错误,因此解决了玉石抛出的错误,但是我仍然发现 products:[]
已被删除很奇怪
Because at some point the empty array was removed from the object. Like I said, I did fix this in the front end, so the error jade was throwing has been addressed, but I still find odd that the products: []
was being removed from the document.
我是MongoDb和node的新手,更不用说我也是JS的新手,所以它很可能是我的一项功能
I'm new to MongoDb and node, not to mention that I'm also new with JS, so it might well be a feature that I'm unaware of.
推荐答案
将空数组传递给Mongo时,它们被解释为空文档{}。 Zend Json编码器会将它们解释为空数组[]。我了解无法确定哪一个是正确的。
如果数组为空,请尝试以
When passing empty arrays to Mongo they are interpreted as empty documents, {}. Zend Json encoder will interpret them as empty arrays []. I understand that it's not possible to tell which one is correct. Incase of empty arrays try posting as
Array [null];
而不是Array [];
这会很好
这篇关于如何使用JS将空数组保存到mongodb中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!