aggregation-framework

How to get the documents based on Date Filters(Week, Month and Custom Dates) in MongoDB?

Deadly 提交于 2021-01-04 06:36:30
问题 I am trying to fetch all the documents based on date filters as below in MongoDB: This Week only Last Week only This Month only Last Month only Between Custom Dates. My collection: [ { _id: "123", status: "seen", userId: "589", createdAt: ISODate("2020-11-17T04:35:29.646Z"), }, { _id: "223", status: "seen", userId: "589", createdAt: ISODate("2020-11-17T04:35:29.646Z"), }, { _id: "474", status: "unseen", userId: "589", createdAt: ISODate("2020-11-10T04:35:29.646Z"), }, { _id: "875", status:

mongodb aggregate $lookup vs find and populate

房东的猫 提交于 2021-01-04 04:57:10
问题 I have a Video Schema like this: const VideoSchema = new mongoose.Schema({ caption: { type: String, trim: true, maxlength: 512, required: true, }, owner: { type: mongoose.Schema.ObjectId, ref: 'User', required: true, }, // some more fields comments: [{ type: mongoose.Schema.ObjectId, ref: 'Comment', }], commentsCount: { type: Number, required: true, default: 0, }, }, { timestamps: true }); and a simple Comment schema like this: const CommentSchema = new mongoose.Schema({ text: { type: String,

mongodb aggregate $lookup vs find and populate

天涯浪子 提交于 2021-01-04 04:54:28
问题 I have a Video Schema like this: const VideoSchema = new mongoose.Schema({ caption: { type: String, trim: true, maxlength: 512, required: true, }, owner: { type: mongoose.Schema.ObjectId, ref: 'User', required: true, }, // some more fields comments: [{ type: mongoose.Schema.ObjectId, ref: 'Comment', }], commentsCount: { type: Number, required: true, default: 0, }, }, { timestamps: true }); and a simple Comment schema like this: const CommentSchema = new mongoose.Schema({ text: { type: String,

Mongo aggregation vs Java for loop and performance

ε祈祈猫儿з 提交于 2021-01-03 06:53:21
问题 I have a below mongo document stored { "Field1": "ABC", "Field2": [ { "Field3": "ABC1","Field4": [ {"id": "123" }, { "id" : "234" }, { "id":"345" }] }, { "Field3": "ABC2","Field4": [ {"id": "123" }, { "id" : "234" }, { "id":"345" }] }, { "Field3": "ABC3","Field4": [{ "id":"345" }] }, ] } from the above, I want to fetch the subdocuments which is having id "123" ie. { "Field3" : "ABC1", "Field4" : [ { "id": "123"} ] } , { "Field3" : "ABC2", "Field4" : [ { "id": "123"} ] } 1. Java way A. use

Getting ancestors in MongoDb using tree structure

亡梦爱人 提交于 2021-01-01 13:07:09
问题 I have the following collection representing the directory structure. I'd like to write an aggregation query that will return all child directory IDs starting from a certain point . For example: Starting from root : results: child_A , child_B , child_A_1 Starting from child_a : results: child_A_1 I've created the following query (in Mongo 4.2) according to the docs, but I'm unable to run it successfully. [{ $match: { _id: ObjectId('5de7a00bf3663d0805644b91') } }, { $graphLookup: { from:

Getting ancestors in MongoDb using tree structure

穿精又带淫゛_ 提交于 2021-01-01 13:03:24
问题 I have the following collection representing the directory structure. I'd like to write an aggregation query that will return all child directory IDs starting from a certain point . For example: Starting from root : results: child_A , child_B , child_A_1 Starting from child_a : results: child_A_1 I've created the following query (in Mongo 4.2) according to the docs, but I'm unable to run it successfully. [{ $match: { _id: ObjectId('5de7a00bf3663d0805644b91') } }, { $graphLookup: { from:

Getting ancestors in MongoDb using tree structure

拜拜、爱过 提交于 2021-01-01 13:02:49
问题 I have the following collection representing the directory structure. I'd like to write an aggregation query that will return all child directory IDs starting from a certain point . For example: Starting from root : results: child_A , child_B , child_A_1 Starting from child_a : results: child_A_1 I've created the following query (in Mongo 4.2) according to the docs, but I'm unable to run it successfully. [{ $match: { _id: ObjectId('5de7a00bf3663d0805644b91') } }, { $graphLookup: { from:

Getting ancestors in MongoDb using tree structure

為{幸葍}努か 提交于 2021-01-01 13:00:26
问题 I have the following collection representing the directory structure. I'd like to write an aggregation query that will return all child directory IDs starting from a certain point . For example: Starting from root : results: child_A , child_B , child_A_1 Starting from child_a : results: child_A_1 I've created the following query (in Mongo 4.2) according to the docs, but I'm unable to run it successfully. [{ $match: { _id: ObjectId('5de7a00bf3663d0805644b91') } }, { $graphLookup: { from:

Getting ancestors in MongoDb using tree structure

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-01 12:56:23
问题 I have the following collection representing the directory structure. I'd like to write an aggregation query that will return all child directory IDs starting from a certain point . For example: Starting from root : results: child_A , child_B , child_A_1 Starting from child_a : results: child_A_1 I've created the following query (in Mongo 4.2) according to the docs, but I'm unable to run it successfully. [{ $match: { _id: ObjectId('5de7a00bf3663d0805644b91') } }, { $graphLookup: { from:

MongoDB: Aggregate and flatten an array field

不羁岁月 提交于 2020-12-31 06:22:47
问题 After 28 years working with relational databases (SQL Server, MySQL, Oracle, Informix) I have moved to MongoDB. It has been slow going over the last two weeks. I would like to submit a couple of questions to confirm my thoughts. My document looks like the following (ignore groupings for this question): { "_id": "xyz-800", "site": "xyz", "user": 800, "timepoints": [ {"timepoint": 0, "a": 1500, "b": 700}, {"timepoint": 2, "a": 1000, "b": 200}, {"timepoint": 4, "a": 3500, "b": 1500} ],