Is there any way to use a dynamic key with node-mongodb-native?

后端 未结 2 1707
没有蜡笔的小新
没有蜡笔的小新 2021-01-06 00:09

Is there any way to use dynamic keys with the node-mongodb-native driver? By this I mean having a variable hold the key instead of using the key directly.

As in when

2条回答
  •  甜味超标
    2021-01-06 01:02

    You can write a small function that creates such an object

    var obj = function(key, value, obj) {
      obj = obj || {};
      obj[key] = value;
      return obj;
    }
    
    // Usage
    foo = obj('foo' + 'bar', 'baz');
    // -> {'foobar': 'baz'}
    
    foo = obj('stack' + 'over', 'flow', foo);
    // -> {'foobar': 'baz', 'stackover': 'flow'}
    

    Don't know if calling this function obj is a good idea.

提交回复
热议问题