如何在MongoDB集合中插入一组键值对?

时间:2015-03-05 00:03:36

标签: javascript mongodb mongodb-query

我在哈希表中以嵌套键/值对的形式具有以下结构。让我们说我在MongoDB命名结构中有一个集合。这里的关键是NAME。如何在FIELDS中插入键/值对:{key:' value',key:' value'进入这个结构集合?

  var input 4 = {   
{ OBJ_TYPE: 'Paris',    
  NAME: 'Paris.Name,
  FIELDS:
  { UNAME: 'STRING', // key is NAME, value is STRING
   DESC: 'STRING' // key is DESC, value is STRING
  }
} 


{ OBJ_TYPE: 'London', 
  NAME: 'London.Name,  
   FIELDS:
    { UNAME: 'STRING',
     DESC: 'STRING'
   }
}

 { OBJ_TYPE: 'X',
    NAME: 'X.XName,
    FIELDS:
   { UNAME: 'STRING',
     DESC: 'STRING'
   }
 }

}

 var test = Message.init(input4);
 var hashTable = test.hash_table;

if(hashTable["OBJECT_TYPE"] == "FEED"){
   for(var key in hashTable){
          if(key == "FIELDS"){
                 var keyHashTable = hashTable[key];
                 for(var column in keyHashTable){
                       var key;
                       var columns = new Array();
                       if(column == "NAME"){
                              key = column;
                              print("key"+key);
                       }else{
                              print("column"+column);
                       }

           structure.update(
                         {key:NAME},
                        *{$set:{/*some dynamic variable or logic here */ }},*
                        {upsert:true},
                        function(err,data){
                              if (err){
                              console.log(err);
                           }else{
                               //console.log("Successfully upserted");
                        }
                    });

我希望MongoDB像这样存储。

NAME(key)     | UNAME i.e. FIELDS 1  |  DESC i.e. FIELDS 2 |
___________________________________________________________|
Paris.Name    |                      |                     |
London.Name   |                      |                     | 
X.Xname       |                      |                     |

0 个答案:

没有答案