动态命名 mongo 关键字段 [英] dynamically name mongo key field
本文介绍了动态命名 mongo 关键字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据变量动态命名一个 mongo 键.例如,
I want to name a mongo key dynamically based on a variable. For instance,
MyCollection.update({_id: id},
{
$set:
{
getsNamedDynamically: {
//stuff
}...
我想在一个变量中设置 getsNamedDynamically
的值,以便根据其他一些因素为该字段分配一个不同的键名.我试过这样做,但是,mongo 从字面上获取值(即作为字符串)并且不会费心在程序中查找变量的值.
I would like to set the value of getsNamedDynamically
in a variable to assign a different key name to the field based on some other factors. I've tried doing this, however, mongo takes the value literally (i.e. as a string) and does not bother to find the variable's value in the program.
推荐答案
您是否尝试过这种方法?(不知道是否有效)
Did you tried this aproach?(dont know if it works)
var dataToSet={};
dataToSet['field']= "some computed value";
然后
MyCollection.update({_id: id},
{
$set: dataToSet
}
这篇关于动态命名 mongo 关键字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文