mongodb-query

How to get percentage total of data with group by date in MongoDB

六眼飞鱼酱① 提交于 2021-02-11 05:57:55
问题 How to get percentage total of data with group by date in MongoDB ? Link example : https://mongoplayground.net/p/aNND4EPQhcb I have some collection structure like this { "_id" : ObjectId("5ccbb96706d1d47a4b2ced4b"), "date" : "2019-05-03T10:39:53.108Z", "id" : 166, "update_at" : "2019-05-03T10:45:36.208Z", "type" : "image" } { "_id" : ObjectId("5ccbb96706d1d47a4b2ced4c"), "date" : "2019-05-03T10:39:53.133Z", "id" : 166, "update_at" : "2019-05-03T10:45:36.208Z", "type" : "image" } { "_id" :

how to update a number field using another number field in MongoDB

会有一股神秘感。 提交于 2021-02-11 04:51:38
问题 Can MongoDB allow updating a number field using another number field in same document ? Let say I have a document like below : { "_id" : "1", "a" : 2, "b" : 3 } a and b fields are number fields(decimal,int...) my goal is update field b using field a How can I set b like b = 2*a . Is this possible ? 回答1: Note : On v 3.6 you don't have an option for direct update, You need to first read & process in code, then update. For v >= 4.2 you can do it in one call as .update() will accept aggregation

how to update a number field using another number field in MongoDB

亡梦爱人 提交于 2021-02-11 04:51:11
问题 Can MongoDB allow updating a number field using another number field in same document ? Let say I have a document like below : { "_id" : "1", "a" : 2, "b" : 3 } a and b fields are number fields(decimal,int...) my goal is update field b using field a How can I set b like b = 2*a . Is this possible ? 回答1: Note : On v 3.6 you don't have an option for direct update, You need to first read & process in code, then update. For v >= 4.2 you can do it in one call as .update() will accept aggregation

how to update a number field using another number field in MongoDB

允我心安 提交于 2021-02-11 04:51:07
问题 Can MongoDB allow updating a number field using another number field in same document ? Let say I have a document like below : { "_id" : "1", "a" : 2, "b" : 3 } a and b fields are number fields(decimal,int...) my goal is update field b using field a How can I set b like b = 2*a . Is this possible ? 回答1: Note : On v 3.6 you don't have an option for direct update, You need to first read & process in code, then update. For v >= 4.2 you can do it in one call as .update() will accept aggregation

How to get last document of each day in MongoDB collection?

余生颓废 提交于 2021-02-10 14:53:46
问题 I have a model Entry to which includes details of a hospital at a particular time. The data looks like this: { "_id": "5ef9c7337874820008c1a026", "date": 1593427763640, //... some data "hospital": { "_id": "5ef8d06630c364000840bb6d", "name": "City Hospital", //... some data }, } I want to get the last query of each day grouped by the hospital ID. In MySQL, it can be achieved using INNER JOIN. How can I do it using MongoDB? 回答1: Given a day, calculate start and end of a day. This is to be used

$lookup when foreignField is array

爷,独闯天下 提交于 2021-02-10 14:41:03
问题 I have 2 collections, the first storing the animes watched by each user, their status etc: const listSchema = mongoose.Schema({ user: { type: Schema.Types.ObjectId, ref: 'user' }, animes: [{ _id: false, anime: { type: Schema.Types.ObjectId, ref: 'anime', unique: true }, status: String, episodes: Number, rating: Number }] }); and the second storing all animes and the relevant information. I want to show that second collection but adding status and episodes fields that are filled from the list

$lookup when foreignField is array

守給你的承諾、 提交于 2021-02-10 14:40:49
问题 I have 2 collections, the first storing the animes watched by each user, their status etc: const listSchema = mongoose.Schema({ user: { type: Schema.Types.ObjectId, ref: 'user' }, animes: [{ _id: false, anime: { type: Schema.Types.ObjectId, ref: 'anime', unique: true }, status: String, episodes: Number, rating: Number }] }); and the second storing all animes and the relevant information. I want to show that second collection but adding status and episodes fields that are filled from the list

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.

mongodb remove document if array count zero after $pull in a single query

廉价感情. 提交于 2021-02-08 12:57:16
问题 I have a requirement where my comments schema looks like the following { "_id": 1, "comments": [ { "userId": "123", "comment": "nice" }, { "userId": "124", "comment": "super"} ] } I would like to pull the elements based on the userId field. I am doing the following query comments.update({},{$pull:{comments:{userId:"123"}}}) My requirement is that if the array length became zero after the pull operator I need to remove the entire document for some reason.Is there a away to do this in a single