Compacting mongodb queries

With the mongo shell, I am doing something in a way which I think should be simpler. I make a first request to get a result needed for a second one.

Here is the first command:

MongoDB -prompt-:PRIMARY> db.Collection.find({"_p_unit":"TopCollection$npsV9rp1Gg","Order":2},{"_id":0,"AUDIO":1})
{ "AUDIO" : "audio_file_key_1a2b3c_Voice.bin" }

I use this result in the next command:

MongoDB -prompt-:PRIMARY> db.Collection.updateOne({"_p_unit":"TopCollection$BTDGm5LIcX","Order":2},{"$set":{ "AUDIO" : "audio_file_key_1a2b3c_Voice.bin" }})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
MongoDB -prompt-:PRIMARY>

Now this is my question:

Is there a way to group the two commands above into one?

Though I tried a few ideas coming up my mind, nothing worked.

Hi @Michel_Bouchet ,

It seems that you want to update a value AUDIO in two different documents of the same collection to be the same, however how can the system know the relationship between those two.

It sounds like 2 commands is the smart way, if you need automicity of the two use transactions to do them within one transaction.

In 4.4 you can merge using aggregation and $merge of 2 data sets by $match the value you need filter, add the merging value with $addFields and merge on it to update the second document. But as you can see it is too complex so 2 steps preferred.

Thanks
Pavel

Yes indeed as you write “It is too complex”. Though my problem is quite simple. In the SQL world something like this would work (combining the two commands):

db.Collection.updateOne({"_p_unit":“TopCollection$BTDGm5LIcX”,“Order”:2},{"$set":(db.Collection.find({"_p_unit":“TopCollection$npsV9rp1Gg”,“Order”:2},{"_id":0,“AUDIO”:1}))})

Even if I have to be satisfied with two commands. Is there a way I could use a variable to store the result of the first command and then use it in the second. I tried but it did not really work.

Hi @Michel_Bouchet ,

Of course you can store in a var:

var result = db.Collection.find({"_p_unit":"TopCollection$npsV9rp1Gg","Order":2},{"_id":0,"AUDIO":1})



 db.Collection.updateOne({"_p_unit":"TopCollection$BTDGm5LIcX","Order":2},{"$set":{ "AUDIO" : result.AUDIO}})

Thanks
Pavel

I tried, but it did not work. The result is:
{ “AUDIO” : undefined } I had also tried something slightly different before which also failed.

That was:

var x = db.Collection.find({"_p_unit":"TopCollection$npsV9rp1Gg","Order":2},{"_id":0,"AUDIO":1})

db.Collection.updateOne({"_p_unit":"TopCollection$BTDGm5LIcX","Order":2},{"$set":x})

Hi @Michel_Bouchet ,

Oh ok so there is a different behaviour with mongo shell, as the query return a pointer to a cursor.

In the shell to overcome this there are 2 options

1:

var result = db.Collection.find({"_p_unit":"TopCollection$npsV9rp1Gg","Order":2},{"_id":0,"AUDIO":1}).toArray()

db.Collection.updateOne({"_p_unit":"TopCollection$BTDGm5LIcX","Order":2},{"$set": result[0]})

2:

var result = db.Collection.find({"_p_unit":"TopCollection$npsV9rp1Gg","Order":2},{"_id":0,"AUDIO":1})
 var updateDoc  = result.next();
 db.Collection.updateOne({"_p_unit":"TopCollection$BTDGm5LIcX","Order":2},{"$set":updateDoc});

Thanks
Pavel