mongodb

How to use variables in MongoDB query?

∥☆過路亽.° 提交于 2021-02-10 06:48:12
问题 Is it possible to use a variables in MongoDB's query like this? var FOO="123"; var BOO="323"; user.findOneAndUpdate({_id: userID},{$set:{FOO:BOO}} BOO - the value, is working fine and it's being assign, while FOO - the key is not. 回答1: In MongoDB as such no functionality but you can do this in javascript like this var FOO="123"; var BOO="323"; user.findOneAndUpdate({_id: userID},{$set:{[FOO]:BOO}} 来源: https://stackoverflow.com/questions/48136206/how-to-use-variables-in-mongodb-query

C# MongoDB Driver: Can't find the way to run complex query for AnyIn filter in MongoDB

限于喜欢 提交于 2021-02-10 06:42:06
问题 I have a document like this: { "id": "xxxxxxxxxxxx", "groupsAuthorized": [ "USA/California/SF", "France/IDF/Paris" ] } And I have an user that has a list of authorized groups, like for example the following: "groups": [ "France/IDF", "USA/NY/NYC" ] What I'm trying to achieve is to retrieve all documents in the database that the user is authorized to retrieve, essentially I want to be able to check in the list "groupsAuthorized" if one of the group contains a subset of an element of the other

Mongodb aggregate match query with priority on full match

天涯浪子 提交于 2021-02-10 05:38:51
问题 I am attempting to do a mongodb regex query on a field. I'd like the query to prioritize a full match if it finds one and then partials afterwards. For instance if I have a database full of the following entries. { "username": "patrick" }, { "username": "robert" }, { "username": "patrice" }, { "username": "pat" }, { "username": "patter" }, { "username": "john_patrick" } And I query for the username 'pat' I'd like to get back the results with the direct match first, followed by the partials.

Mongoose query for documents from last 24 hours, only one document per hour

风流意气都作罢 提交于 2021-02-10 05:34:08
问题 I am coding an app in which there are some weather sensors that send air's temperature and humidity to the server every 5 minutes. I would like to draw a chart of how the temperature and humidity change i.e overnight. My idea is to draw the chart basing on data from last 24 hours. I cannot figure it out by myself, so I thought maybe I could seek help here. Of course, each measurement document has a field called createdAt which has the timestamp of the moment it has been created. I need to

MongoDB aggregate pipelines with linked object

冷暖自知 提交于 2021-02-10 05:09:44
问题 I'm linking two objects in one query and use aggregate function for it. Some data is localized and I'm using a solution from here to get data for specified locale. I am struggling to do the same with data from the linked object (rooms). Specifically, list data for given locale from the roomDetails. Please take a look at the Mongo playground 回答1: You just need to add filter in your second $addFields stage that you are filtering roomTypes and rest of the stages would be same, just highlighted

MongoDB aggregate pipelines with linked object

本秂侑毒 提交于 2021-02-10 05:09:19
问题 I'm linking two objects in one query and use aggregate function for it. Some data is localized and I'm using a solution from here to get data for specified locale. I am struggling to do the same with data from the linked object (rooms). Specifically, list data for given locale from the roomDetails. Please take a look at the Mongo playground 回答1: You just need to add filter in your second $addFields stage that you are filtering roomTypes and rest of the stages would be same, just highlighted

MongoDB aggregate pipelines with linked object

强颜欢笑 提交于 2021-02-10 05:05:51
问题 I'm linking two objects in one query and use aggregate function for it. Some data is localized and I'm using a solution from here to get data for specified locale. I am struggling to do the same with data from the linked object (rooms). Specifically, list data for given locale from the roomDetails. Please take a look at the Mongo playground 回答1: You just need to add filter in your second $addFields stage that you are filtering roomTypes and rest of the stages would be same, just highlighted

Duplicating PostgreSQL's window functions like lag, lead, over

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-10 04:55:10
问题 How do I change a PostgreSQL query into a mongodb bson call? I have the same use case listed at http://archives.postgresql.org/pgsql-general/2011-10/msg00157.php I would like to calculate the delta time between two log entries by using something like lag or lead. Is there anything similar in mongodb to Postgres' lag / lead syntax? select index, starttime, endtime, starttime - lag(endtime) over(order by starttime asc) as delta from test http://www.postgresql.org/docs/8.4/static/functions

Duplicating PostgreSQL's window functions like lag, lead, over

随声附和 提交于 2021-02-10 04:51:18
问题 How do I change a PostgreSQL query into a mongodb bson call? I have the same use case listed at http://archives.postgresql.org/pgsql-general/2011-10/msg00157.php I would like to calculate the delta time between two log entries by using something like lag or lead. Is there anything similar in mongodb to Postgres' lag / lead syntax? select index, starttime, endtime, starttime - lag(endtime) over(order by starttime asc) as delta from test http://www.postgresql.org/docs/8.4/static/functions

Mongodb php query, search in array?

房东的猫 提交于 2021-02-10 03:26:49
问题 I am looking for a way to search for a keyword (ex. "Henrik") in the "answers" array of documents with the following structure Array ( [_id] => MongoId Object ( [$id] => 4eeedd9545c717620a000007 ) [formId] => 6 [respId] => 4eeedd95c93228 [dateCreated] => 2011-10-14 07:45 [answers] => Array ( [field1] => Henrik [field6] => myemail@domain.com [field7] => my city address ) ) I am working in PHP on this project, and quering like this works of course: $answers = $collection->find( array('formId' =