RejectedSoftware Forums

Sign up

Mongo update only certain fields

Hello !

The actual Mongodb driver do not allow the update of only the fields provided but mongodb itself provides that capability.

There is plans to add this functionality to Mongodb driver ?

Cheers !

Re: Mongo update only certain fields

On Mon, 20 Oct 2014 22:16:48 GMT, Domingo wrote:

Hello !

The actual Mongodb driver do not allow the update of only the fields provided but mongodb itself provides that capability.

There is plans to add this functionality to Mongodb driver ?

Cheers !

you can update individual fields as follows:

collection.update(
  `{"_id": 1234}`,
  `"$set": {
    {"field1": 532},
    {"field2": "hello"}
  }`
);

Re: Mongo update only certain fields

Nice to know that !

The only problem with this is we should escape fileds by hand, a transparent solution would be better.

Cheers !

On Tue, 21 Oct 2014 05:28:52 GMT
"Jack Applegame" japplegame@gmail.com wrote:

On Mon, 20 Oct 2014 22:16:48 GMT, Domingo wrote:

Hello !

The actual Mongodb driver do not allow the update of only the fields provided but mongodb itself provides that capability.

There is plans to add this functionality to Mongodb driver ?

Cheers !

collection.update(
  `{"_id": 1234}`,
  `"$set": {
    {"field1": 532},
    {"field2": "hello"}
  }`
);


Domingo Alavarez Duarte mingodad@gmail.com

Re: Mongo update only certain fields

I was wrong about need to escape fields manually.

alias strrec = string[string];
str
rec[string] rec;
rec["$set"] = record;
collection.update(["id":Bson(BsonObjectID.fromString(theid))], rec);

On Tue, 21 Oct 2014 08:02:21 -0700
Domingo Alavarez Duarte mingodad@gmail.com wrote:

Nice to know that !

The only problem with this is we should escape fileds by hand, a transparent solution would be better.

Cheers !

On Tue, 21 Oct 2014 05:28:52 GMT
"Jack Applegame" japplegame@gmail.com wrote:

On Mon, 20 Oct 2014 22:16:48 GMT, Domingo wrote:

Hello !

The actual Mongodb driver do not allow the update of only the fields provided but mongodb itself provides that capability.

There is plans to add this functionality to Mongodb driver ?

Cheers !

collection.update(
  `{"_id": 1234}`,
  `"$set": {
    {"field1": 532},
    {"field2": "hello"}
  }`
);


Domingo Alavarez Duarte mingodad@gmail.com

Domingo Alavarez Duarte mingodad@gmail.com

Re: Mongo update only certain fields

On Tue, 21 Oct 2014 09:11:15 -0700, Domingo Alavarez Duarte wrote:

I was wrong about need to escape fields manually.

Yes. Another posibility, if values have different types (not only strings):

collection.update(
  ["_id": id], ["$set": [
      ["field1": value1.serializeToBson],
      ["field2": value2.serializeToBson],
      ["field3": value3.serializeToBson]
  ]]
);