PMXBOT Log file Viewer

Help | Karma | Search:

#mongodb logs for Tuesday the 3rd of April, 2018

(Back to #mongodb overview) (Back to channel listing) (Animate logs)
[08:47:38] <littlekitty> hi how can I update my documents according to a given container (array/list)?
[08:48:07] <littlekitty> something like this pseudo code: if document.id in list: document.update
[08:48:49] <littlekitty> but without having to iterate the list
[08:49:22] <Derick> what sort of update do you need to do?
[08:50:45] <littlekitty> what sorts are there?
[08:51:10] <Derick> do you need to update depending on something?
[08:51:20] <littlekitty> yes
[08:51:25] <littlekitty> also, I'm using motor
[08:51:30] <littlekitty> for asyncio
[08:51:35] <Derick> Sorry, can't help you with motor
[08:51:41] <Derick> but it's quite possible to do updates like:
[08:52:07] <Derick> db.colname.update( { _id: { '$in': list } }, { $set: { 'field' : 42 } } ).
[08:52:17] <Derick> db.colname.update( { _id: { '$in': list } }, { $set: { 'field' : 42 } } );
[08:52:31] <Derick> which will set the field "field" to 42 for all documents where _id is in the list
[08:53:07] <littlekitty> oh great, this looks like what I need
[08:53:19] <Derick> don't know the motor syntax though
[08:53:20] <littlekitty> but I'm confused, do I need to use update_many in python ?
[08:53:31] <Derick> yeah, probably
[08:53:32] <littlekitty> it's fine I can use pymongo for now too
[08:53:45] <Derick> my shell line was wrong too, it should have been:
[08:53:52] <Derick> db.colname.update( { _id: { '$in': list } }, { $set: { 'field' : 42 } }, { multi: true } );
[08:54:01] <Derick> and the shell also does an updateMany now
[08:54:06] <Derick> so yeah, use that :)
[08:54:18] <littlekitty> ok I'll give it a try and report back
[08:54:21] <littlekitty> thank you Derick
[10:17:43] <littlekitty> Derick: thank you I think it works now
[10:17:45] <littlekitty> collection.update_many({'tar_adr': {'$in': list(_dict)}}, {'$set': {'swapped': True}})
[10:18:35] <Derick> that looks about right! glad you got it working
[10:19:32] <littlekitty> one more thing, do you know if it's alright to use the result of a find operation as a collection for a python's for loop?
[10:19:39] <littlekitty> e.g.: for document in collection.find({'$and': [{'confirmations': {'$gte': 6}}, {'swapped': False}]}):
[10:20:54] <littlekitty> (btw I hope that will give me all the documents where 'confirmations' is greater than or equal 6 and the boolean of 'swapped' is false)
[10:20:59] <Derick> sure, that should work
[10:21:07] <Derick> you don't need "$and" though, as that's the default
[10:21:13] <Derick> this should do the trick:
[10:21:47] <Derick> collection.find({'confirmations': {'$gte': 6},'swapped': False})
[10:22:50] <littlekitty> oh okay good to know, thank you I changed it
[11:11:17] <Yukkuri> hi, how do i express joins in mongodb?
[11:12:06] <Yukkuri> say, i have in my collection a foreignkey ID pointing to other collction, how do i join them in plain collection, so i can sort on fields of joined collections?
[11:12:40] <Yukkuri> say, A { name: STRING, bar: B_ID }; B { name: STRING }
[11:12:49] <Yukkuri> how do i query on A, so i can osrt on B.name ?
[11:12:52] <Yukkuri> sort*
[11:13:24] <Yukkuri> in SQL it can be done with select * from A left join B on A.id = B.id order by B.name;
[11:13:35] <Yukkuri> err
[11:13:41] <Yukkuri> A.bar = B.ID
[11:13:56] <Yukkuri> how to do the same in mongo?
[11:49:56] <Derick> MongoDB has no SQL joins, but there is $lookup in the aggregation framework