PMXBOT Log file Viewer

Help | Karma | Search:

#mongodb logs for Thursday the 12th of March, 2020

(Back to #mongodb overview) (Back to channel listing) (Animate logs)
[00:11:40] <cgi> kali, thanks
[05:48:37] <edmunddantes> Hi, I can't find mongo binary
[05:49:22] <edmunddantes> I've installed mongodb, but when I try `mongo`, it says command not found
[14:26:12] <r0ph> Hey all... simple question (I think)... I'm trying to run an updateMany() on a field that's null, and set it to another field in the document.
[14:27:22] <r0ph> I read that it should work something like this: db.getCollection("collection").updateMany({"fieldname": null}, { $set: {"fieldname": document.otherFieldName}})
[14:27:33] <r0ph> but I get a "document is not defined" error
[14:28:54] <r0ph> trying to do the equivalent of UPDATE table SET field=otherfield WHERE field IS NULL
[14:36:57] <kali> r0ph: yeah, you can't refer to other fields in update. maybe you can use the $rename moidifer though: https://docs.mongodb.com/manual/reference/operator/update/rename/
[14:37:45] <r0ph> kali: thank you... but what about the answer here? https://stackoverflow.com/questions/3974985/update-mongodb-field-using-value-of-another-field
[14:38:16] <kali> pipeline merge can work yes
[14:39:28] <kali> ho, did not know pipeline ops could be used in update, this is relativeley recent
[14:40:59] <r0ph> how would that pipeline bit help me to do what i'm trying to do?
[14:41:11] <r0ph> i'd think that this would be a relatively common operation, no?
[14:45:00] <kali> r0ph: pipeline refers to the aggregation pipeline. i did not know about the "4.2+" option, where you use aggregation pipeline modifiers directly in the update instruction
[14:46:30] <r0ph> yeah, im on the current release (4.2.4)... so i'm hoping that it could help me
[14:47:25] <r0ph> kali: as you can probably tell, I'm new to MongoDB... I'm not even sure what an aggregation pipeline is... is that something I could use to loop through results and make updates to selected records?
[14:47:38] <kali> well, the difference here is you can refer to the field in the document with the $ sign
[14:48:03] <kali> the old update did not allow you to do that
[14:48:50] <r0ph> when I tried an update like this: db.getCollection("collection").updateMany({"fieldname": null}, { $set: {"fieldname": "$otherFieldName"}}) <-- it just sets "fieldname" to the string "$otherFieldName", and not the value of that field.
[14:48:52] <kali> so you can do { "$set": { "newfieldname": "$oldfieldname" }}
[14:50:29] <kali> how about ? { "$set": { "newfieldname": { "$concat" : "$oldfieldname" } }}
[14:50:37] <kali> how about ? { "$set": { "newfieldname": { "$concat" : [ "$oldfieldname" ] } }}
[14:50:52] <kali> there is probably a better option than $concat...
[14:51:12] <r0ph> yeah, I saw that... since i'm not concating anything I figured it didn't apply
[14:51:17] <r0ph> i'll give it a try. thank you!
[14:52:43] <kali> basically we may need something in the shape of an op for the $ trick to work (i'm not sure)
[14:58:26] <r0ph> kali: "The dollar ($) prefixed field '$concat' in 'fieldname.$concat' is not valid for storage.",
[15:50:37] <r0ph> kali: db.collection.aggregate([{$match: {field: null}},{$set: {field: "$otherField"}}]); <-- this may work, but setting it in the aggregate doesn't set it in the actual collection, right?
[15:50:40] <r0ph> just in the results?
[15:51:04] <kali> yes
[15:51:40] <kali> what the doc says is that it should also work in update, but you know as much as i do about this :)