A
A
Artem Komarov2014-05-19 12:23:43
JavaScript
Artem Komarov, 2014-05-19 12:23:43

How to update using compound field name in MongoDB via NodeJS?

there is a table like this:

{
  "_id" : ObjectId("5379b1b81cdcaf4db70cc16f"),
  "hash" : "123",
  "serverList" : {
    "server1" : {
      "host": "http://192.168.5.138:80/",
                        "url" : "/123/",
      "ssid" : "321"
    },
    "server2" : {
      "host": "http://192.168.5.144:80/",
                        "url" : "/123/",
      "ssid" : "321"
    }
  }
}

It is sometimes necessary to update the ssid and url fields. I'm trying to do this:
var data = JSON.parse(params).result, ssid = 'serverList.'+server[i]+'.ssid', url = 'serverList.'+server[i]+'.url';
leaders.update({'hash':request.cookies.hash}, {'$set':{ ssid: data['ssid'], url: data['sessionurl']}}, function(e, d) {
  console.dir(e);
  console.dir(d);
});
// Где server[i] это 'server1' или 'server2', а в data приходят нужные url и ssid

But, as far as I understand, the string that is contained in ssid and url is not transmitted, but stupidly ssid and url are transmitted as a field value. How to correctly compose the query string so that update takes place on the desired (dynamically generated) path?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Komarov, 2014-05-19
@m0sk1t

So far I decided to form a change request as an object ...

var $set = {};
$set['$set'] = {};
$set['$set']['serverList.'+i+'.ssid'] = data['ssid'];
$set['$set']['serverList.'+i+'.url'] = data['sessionurl'];

And pass this object in the request body. Are there more beautiful and elegant solutions?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question