流星集合中的数字字段名称 [英] Numeric field names in meteor collection
本文介绍了流星集合中的数字字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要实现这样的集合结构:
I need to achieve such collection structure:
{
"_id" : "36JaTS8N9uJDHabaq",
"pages" : {
1 : {
"field1" : "value1",
"field2" : "value2"
},
...
}
}
一切都很好,当我执行以下操作时:
Everything is fine, when I'm doing the following:
Collection.insert({
"pages": {
1: {
"field1" : "value1",
"field2" : "value2"
}
}
})
但是当我尝试使用包含数字的变量而不是数字本身时,例如:
But when I'm trying to use the variable containing the number instead of number itself, like:
var number = 1;
Collection.insert({
"pages": {
number: {
"field1" : "value1",
"field2" : "value2"
}
}
})
我没有收到我想要的结果(数字应该是变量名):
I receive not the result I want (there is a variable name where number should be):
{
"_id" : "SjMotZHrtXviuoyqv",
"pages" : {
"number" : {
"field1" : "value1",
"field2" : "value2"
}
}
}
完成我正在努力实现的目标的正确方法是什么?
What is the right way to do what I'm trying to achieve?
提前致谢!
推荐答案
在 JavaScript 中,您不能将变量用作对象字面量的键.见 这个答案.
You can't use variables as keys in object literals in javascript. See this answer.
var number = 1;
var pages = {};
pages[number] = {f1: 'v1', f2: 'v2'};
Collection.insert({pages: pages});
这篇关于流星集合中的数字字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文