[20:57:07] <asdfgh> guys i have just started with mongodb i need to creat documents where users can define their fields and values, the problem is that i do not know how can i index such fields because an user can have many fields with different names etc
[22:47:18] <GothAlice> https://gist.github.com/amcgregor/547d35feb9cf41351373 ← if your users can "model" data (i.e. define the fields they care about) one approach becomes dynamic modelling. This can be used to construct a model whose definition is itself stored in the database (or any other serialization, which a database can be considered). Including indexes. With the note that these types of models should change extremely rarely, and extremely carefully.
[22:48:35] <GothAlice> A second approach to storing truly dynamic data is the embedded associative array. Not an embedded document whose keys are dynamic, that's tragic. :sad bass note slide: An array of embedded documents that are awesome. :happy bass note slide:
[22:50:15] <GothAlice> You can then index properties.value to index the values for easy searching, and properties.key to quickly identify which documents have which dynamic keys, if needed.
[22:50:24] <asdfgh> yes i thought a similar solution {_id: ObjectId(…), properties: [{key: "username", value: "GothAlice"}, {key: "age", value: "yarite"}, …]} <------
[22:51:04] <asdfgh> GothAlice, what index i can create? i mean, should i use ONE index with those two prorperties key/value or two distinct indexes ?
[22:51:21] <GothAlice> Note, to update these values you are now restricted to only updating one at a time atomically ($elemMatch to find the key back, $set properties.$.value to update), or whole-document update/replacement, which is extremely gross.
[22:51:49] <GothAlice> $set or any other operation, but addressed like that was. Silly examples being over-specific.
[22:52:15] <GothAlice> The index you create will be based on your needs.
[22:52:30] <asdfgh> yes but can i create one index for two fields?
[22:53:54] <asdfgh> properties.key and properties.value
[22:55:24] <GothAlice> If you only care about searching the values, but don't really care which "property" they came from, just properties.value. If you need to identify which documents contain which keys, e.g. find only documents that have a "username" property, then properties.name. If you need to be able to search for specific pairs, either index (properties.name, properties.value) [this makes properties.name "indexed" as well, since it can re-use "index
[22:55:24] <GothAlice> prefixes"] but also lets you search for specific property value matches.
[22:55:39] <GothAlice> Or, if you don't care so much for the re-use of index prefixes, just index "properties"; then you must query using documents, and anything other than exact matches becomes problematical, so that's less recommended, but likely useful for some situations.
[22:58:36] <asdfgh> GothAlice, how can i create the query that match multiple properties? i mean something like {'key': 'name', 'value': 'john'....'key': 'age', 'value': 10}?
[22:58:55] <asdfgh> how can i pass such parameters in find()
[23:04:26] <GothAlice> You'd search this via find({properties: {name: "username", value: "GothAlice"}}), remembering that find({properties.name: "username", properties.value: "GothAlice"}) doesn't mean the username property must equal GothAlice, it means there must be a property whose name is username, and any other property (or the same one) whose value is "GothAlice".
[23:04:32] <GothAlice> Sorry, got distracted by migrating mother.
[23:05:15] <asdfgh> but the problem is that i must find more "key"
[23:05:35] <asdfgh> key(name) and key(age) and key(birthdate) etc etc
[23:05:38] <GothAlice> Since properties is an array, you can use all of the array querying operators: https://docs.mongodb.com/manual/tutorial/query-array-of-documents/
[23:07:14] <asdfgh> pardon GothAlice maybe because it is late night :D
[23:07:20] <asdfgh> but in the page you linked me i see { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 }
[23:07:34] <GothAlice> https://gist.github.com/amcgregor/7821f7de8b711dcad6e7685f5637703e is the little quick reference I made for myself, with comparisons to set notation and operations.
[23:08:04] <asdfgh> how can i find DOCUMENTS that have A.qty=40 >> AND << B.qty = 5
[23:08:06] <GothAlice> To query multiple properties, you'd want what you see on line 35. Just with embedded document key/value pairs rather than strings. :3