mongodb-query

Mongo DB aggregation with array of objects

让人想犯罪 __ 提交于 2020-05-17 07:42:21
问题 My collection schema is as follows: Product { _id: ObjectId(), // default mongo db id specification: [ {key: 'Name', value: "value 1"}, {key: 'Category', value: "value 2"}, {key: 'Department', value: "value 3"} ] } Now I want to query on this with a generic filter. For example, Get me all product with Name = value 1 and Category in [value 2, value 3] and Department = value 3 Get me all product with Name = value 1 or Category = value 2 or Department in [value 3, value 4] I have been trying to

Mongo DB aggregation with array of objects

删除回忆录丶 提交于 2020-05-17 07:42:10
问题 My collection schema is as follows: Product { _id: ObjectId(), // default mongo db id specification: [ {key: 'Name', value: "value 1"}, {key: 'Category', value: "value 2"}, {key: 'Department', value: "value 3"} ] } Now I want to query on this with a generic filter. For example, Get me all product with Name = value 1 and Category in [value 2, value 3] and Department = value 3 Get me all product with Name = value 1 or Category = value 2 or Department in [value 3, value 4] I have been trying to

Mongo aggregate query results in less document with sorting

随声附和 提交于 2020-05-16 22:40:46
问题 I have faced a weird issue while querying one of our collections and aggregating the result out to another collection. I am querying unique users who have done some action and projecting the count of action performed per user in the aggregation query. var result = db.getCollection('user_actions').aggregate( [ {"$match":{"createdAt":{"$gte":1585161000000,"$lt":1585247340000}}}, {"$group":{"_id":{"accountId":"$user.id"},"count":{"$sum":1}}}, {"$sort": {"count": -1}}, {"$project":{"_id":0,"count

Using an Index with Mongo's $first Group Operator

十年热恋 提交于 2020-05-16 22:00:20
问题 Per Mongo's latest $group documentation, there is a special optimization for $first: Optimization to Return the First Document of Each Group If a pipeline sorts and groups by the same field and the $group stage only uses the $first accumulator operator, consider adding an index on the grouped field which matches the sort order. In some cases, the $group stage can use the index to quickly find the first document of each group. It makes sense, since only the first entry in an ordered index

MongoDB Aggregate $lookup on array of object with additional field

岁酱吖の 提交于 2020-05-16 06:26:48
问题 I have two different collections (example below) methods & items . As for now, I'm using pre 3.6 vanilla aggregation query for $lookup : MongoPlayground Example { $lookup: { from: "items", localField: "reagents._id", foreignField: "_id", as: "reagent_items" } } The problem is that if I am using it, I miss quantity field (from methods.reagents embedded) during $lookup stage from original collection. For now, I return quantity right after lookup but as I heard, Mongo introduced from 3.6 a new

MongoDB Aggregate $lookup on array of object with additional field

心不动则不痛 提交于 2020-05-16 06:23:28
问题 I have two different collections (example below) methods & items . As for now, I'm using pre 3.6 vanilla aggregation query for $lookup : MongoPlayground Example { $lookup: { from: "items", localField: "reagents._id", foreignField: "_id", as: "reagent_items" } } The problem is that if I am using it, I miss quantity field (from methods.reagents embedded) during $lookup stage from original collection. For now, I return quantity right after lookup but as I heard, Mongo introduced from 3.6 a new

Importing a JSON file in an mongo container using authentication mechanism

前提是你 提交于 2020-05-16 06:18:10
问题 I am using a mongo container and want to insert the records in a JSON file to the mongo container Here is the docker-compose.yml version: '3' services: mongodb: image: mongo ports: - 27017:27017 environment: MONGO_INITDB_ROOT_USERNAME: "dev" MONGO_INITDB_ROOT_PASSWORD: "pass" mongo_seed: build: ./mongo-seed depends_on: - mongodb mongo-seed is another docker container which uses mongoimport to load the data in the database FROM mongo COPY services.json /services.json CMD mongoimport --host

Can I add index dynamically to my schema in Mongoose?

无人久伴 提交于 2020-05-16 03:28:10
问题 For example I have the following structure of documents: { "subject":"Joe owns a dog", "content":"Dogs are man's best friend", "likes": 60, "year":2015, "language":"english" } And the schema would be: var schema = new Schema({ subject: {type: String}, content: {type: String}, likes: {type: int}, year: {type: int}, language: {type: String} } schema.createIndex({"subject": "text"}); Can I dynamically remove the index and create another one with more fields? So if now it searches for a keyword

MongoDB - How to return matched item in an array while using $in in $group?

纵然是瞬间 提交于 2020-05-16 02:19:21
问题 I'm using MongoDB's aggregation. Using $in I'm checking if a value exists in an array or not, And I want to return the matched object from calificacion array into calificacion_usuario field. (my _id is unique in the array). I tried using $first" to return the current element. It's not working, not sure why & what to use. How can I do it? Sample Doc : { "cargo" : "Presidente", "calificacion" : [ { "_id" : "5e894ae6fa9fd23780bcf472", "estrellas" : 3 }, { "_id" : "5e895187fa9fd23780bcf478",

Concat int and string array fields which are in different arrays

半腔热情 提交于 2020-05-15 09:25:53
问题 { "no" : "2020921008981", "date" : ISODate("2020-04-01T05:19:02.263+0000"), "sale" : { "soldItems" : [ { "itemId" : "5b55ac7f0550de00210a3b24", "qty" : NumberInt(1), }, { "itemId" : "5b55ac7f0550de00210a3b25", "qty" : NumberInt(2), } ], "items" : [ { "_id" : ObjectId("5b55ac7f0550de00210a3b24"), unit :"KG" }, { "_id" : ObjectId("5b55ac7f0550de00210a3b25"), unit :"ML" } ] } } Desired output : { "no" : "2020921008981", "sale" : {} "qtyList" : "1 KG \n 2 ML" } In order to build itemQtyList