PMXBOT Log file Viewer

Help | Karma | Search:

#mongodb logs for Friday the 31st of May, 2019

(Back to #mongodb overview) (Back to channel listing) (Animate logs)
[06:45:42] <DnzAtWrk2> yo lo
[06:45:50] <DnzAtWrk2> oh what the hell
[06:48:52] <DnzAtWrk> garbage protocol
[06:49:14] <DnzAtWrk> with mongolab queries, is it possible to filter based on the number of fields?
[12:06:05] <GothAlice> Dang, DnzAtWrk fled.
[12:06:36] <GothAlice> a) Don't have dynamic fields. b) List of embedded documents with name/value combinations. c) $size
[12:11:28] <GothAlice> Async protocol, garbage… timezones? XP
[12:11:35] <murosai> hey guys, im trying to update a document in an array using this code await this.collection.findOneAndUpdate({ id: researchID, documents: { $elemMatch: { id: documentID } } }, { $set: { 'documents.$': document } }, { returnOriginal: false });
[12:11:51] <murosai> problem is that it replaces the whole document, not really update
[12:12:18] <GothAlice> You're $set'ing the entire array element. That's "replace entry" not "update entry".
[12:12:32] <murosai> yeah i figured so, but what's the correct way then
[12:13:05] <GothAlice> Depends entirely on what you actually want to do, which has not been described. $set the sub-fields you care about, or use any other operation on any other sub-field. Just don't target document.$ itself.
[12:14:21] <GothAlice> {$set: {'document.$.name': "Alice"}} for example.
[12:14:22] <murosai> if my document parameter is lets say {description: "foobar"} then i want to only update the description field and leave everything else same
[12:14:32] <GothAlice> See the example I just gave.
[12:14:33] <murosai> i don't want to set each field separetly
[12:14:38] <murosai> separately
[12:14:49] <GothAlice> … then you're replacing and I do not understand the impedance mismatch.
[12:15:36] <murosai> basically i want $set to work like it would if not working with an array
[12:15:43] <GothAlice> "then i want to only update the description field and leave everything else same" is incompatible with "i don't want to set each field separately" — like, what even?
[12:16:18] <murosai> i mean the document object contains all the fields i want to update
[12:18:05] <GothAlice> { $set: { 'documents.$': document } } where document is {'name': "Alice", 'description': "Bob", …}. So, given what we've just outlined about targeting fields within that array, prefix every key in document with 'documents.$.' and use it as {$set: document}, no?
[12:18:10] <GothAlice> A fairly basic pivot.
[12:18:47] <GothAlice> { $set: { 'documents.$': document } } will replace the matching array element, period. {$set: document} with prefixed fields… will set/update them "individually".
[12:19:11] <GothAlice> (Not wholesale replacement, which would drop other fields, for example.)
[12:20:24] <murosai> yeah i could do it that but there must be a more elegant way
[12:21:45] <GothAlice> "The way" — elegant or not hardly matters, no? We can blue-sky all sorts of protocols and plans that if MongoDB doesn't understand them, is entirely a waste of time, right?
[12:22:34] <murosai> if the set of fields would be fixed this approach would be okay, seems like looping the field to construct a query is so clumsy
[12:22:44] <murosai> fields*
[12:22:54] <GothAlice> Okay, then.
[12:25:52] <GothAlice> murosai: http://s.webcore.io/6110e1d980ae/comprehensions-man.png — it's trivial, and fast, but we'll go with "clumsy".
[12:28:03] <GothAlice> (The network RTT—roundtrip time / ping time—is longer than this.)
[12:28:05] <murosai> :) just wondering if there is a built-in way to do it that i am missing, doesn't seem like that uncommon operation
[12:28:27] <GothAlice> The need to do this may indicate poorly structured data.
[12:31:29] <GothAlice> It's a warning sign to slow down and really think about how your data is structured. For example, I hope your fields/keys aren't actually (largely) dynamic document-to-document within the same collection. {a:27, b:"foo"} one document then {c:42, b:96} another gets confusing, and hard to index. {prop: [{k:"a", v:27}, {k:"b", v:"foo"}]} — strangely, easier to index, and easy to update individual dynamically named items by name ($
[12:31:29] <GothAlice> operator, match on prop.k, update $.v), but then you're actually hard limited to updating one field per update invocation.
[12:32:35] <GothAlice> The approach outlined above (prefixing the updated field names with the $ field selector) solves your problem of updating multiple fields at once, without wholesale replacement.