Discussion:
[mongodb-user] How to insert set of key value pair in MongoDB collection?
CloudJedi
2015-03-05 00:05:32 UTC
Permalink
I have the following structure in hash table in the form of nested
key/value pairs. Let's say I have a collection in MongoDB named structure.
How do I insert only the key/value pairs inside FIELDS: { key: 'value',
key: 'value' } into this structure collection?

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:null},
*{$set:{/*some dynamic variable or logic here */ }},*
{upsert:true},
function(err,data){
if (err){
console.log(err);
}else{
//console.log("Successfully upserted");
}
});
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/8427570a-8513-44fb-a456-d6b129242c5b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Will Berkeley
2015-03-06 16:08:05 UTC
Permalink
What is input 4? It's not a valid name for a variable and the value of it
is not in valid syntax for an object. I'm going to assume it's an array and
you meant to write

var input4 = [
{
"OBJ_TYPE" : "Paris",
"NAME" : "Paris.Name",
"FIELDS" : {
"UNAME" : "STRING",
"DESC" : "STRING"
}
},
{
"OBJ_TYPE" : "London",
"NAME" : "London.Name",
"FIELDS" : {
"UNAME" : "STRING",
"DESC" : "STRING"
}
},
{
"OBJ_TYPE" : "X",
"NAME": "X.XName",
"FIELDS": {
"UNAME" : "STRING",
"DESC" : "STRING"
}
}
]

Also, which documents are you trying to update? One with key "key" having
null or no value? Can you show an example of the documents you want to
update based on input4 and what they should look like after the update?

-Will
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/f266a907-7f4c-4c4e-9841-6b5bbdb4f379%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...