Hello, I’m pretty new at working with mongoDB. I’m using it as my database in a small javascript project. I’m looking for a solution to something I’d like to do.
I have a cluster full of userdata, with several properties in each. What would be the best way to retrieve (preferably as an array?) every “id” (not _id) field content, but only where “propertyA” is bigger than “propertyB”. These two properties are both stored as numbers, of course. I’d like to have a list of their id so I can perform an action on all of them in my software, namely to increment propertyB by a number.
If this is something not possible or too complex, getting an array list of EVERY “id” property would be useful aswell.
That shouldn’t be the case, as for example my document already should meet the requirements of the query. Probably should have mentioned that these properties are nested, not sure if it matters
It does meet the query filter requirement. But, the two fields are in an embedded documentstats. So, you need to refer to the two fields as follows in your query (I omitted the remaining parts of the query for brevity):
The picture above is an example. The project is related to a Discord server, people are saved to the database by their Discord ID (“id” field) and have several properties attached to them. Not everyone has the “CurrentHP” and “MaxHP” properties, but most of them do and everyone who does, has it in the format seen on the picture above. There are other properties in the “stats” object too
In the image the currentHP (100) is less than the maxHP (109) - so it will not select the document. The document will get selected when the currentHP is greater then the maxHP. For example, in the following two documents only the document with id: 2 will be selected:
I see, I probably explained it badly, however the opposite is what I’m trying to achieve. I am trying to get every document’s “id” property, where currentHP < maxHP
I tried switching the two property values in the query around, but that still resulted in an empty array.
May be. I see you are using the MongoDB NodeJS driver to work with he database data. Can you write a query to count he number of documents in the collection and tell me about the result.