How to check whether give no lies between these two

{
	"_id" : "62860d15ae6cd23352fa3683",
	"chapterName" : "chap1",
	"groupId" : ObjectId("5f06cca74e51ba15f5167b86"),
	"teamId" : ObjectId("6069a6a9a0ccf704e7f4b537"),
	"topicsList" : {
		"fromDate" : "26-05-2022",
		"fromDateString" : 20220526,
		"toDate" : "29-05-2022",
		"toDateString" : 20220529,
		"topicId" : "62860d15ae6cd23352fa3684",
		"topicName" : "1"
	},
	"userId" : ObjectId("6069a5daa0ccf704e7319d16")
}
{
	"_id" : "62860d15ae6cd23352fa3683",
	"chapterName" : "chap1",
	"groupId" : ObjectId("5f06cca74e51ba15f5167b86"),
	"teamId" : ObjectId("6069a6a9a0ccf704e7f4b537"),
	"topicsList" : {
		"fromDate" : "01-06-2022",
		"fromDateString" : 20220601,
		"toDate" : "03-06-2022",
		"toDateString" : 20220603,
		"topicId" : "62860d15ae6cd23352fa3685",
		"topicName" : "2"
	},
	"userId" : ObjectId("6069a5daa0ccf704e7319d16")
}
{
	"_id" : "62860d15ae6cd23352fa3686",
	"chapterName" : "chap2",
	"groupId" : ObjectId("5f06cca74e51ba15f5167b86"),
	"teamId" : ObjectId("6069a6a9a0ccf704e7f4b537"),
	"topicsList" : {
		"fromDate" : "02-07-2022",
		"fromDateString" : 20220702,
		"toDate" : "04-07-2022",
		"toDateString" : 20220704,
		"topicId" : "62860d15ae6cd23352fa3687",
		"topicName" : "1"
	},
	"userId" : ObjectId("6069a5daa0ccf704e7319d16")
}

I wanted to check fromDateString >= 20220526 and toDateString <= 20220526

the query used is

db.VW_SYLLABUS_PLAN_DB.find({$expr: {$and: [{$gte: ["topicsList.fromDateString", 20220526]}, {$lte: [20220526,"topicsList.toDateString"]}]}}).pretty()

The output I’m getting

{
	"_id" : "62860d15ae6cd23352fa3683",
	"chapterName" : "chap1",
	"groupId" : ObjectId("5f06cca74e51ba15f5167b86"),
	"teamId" : ObjectId("6069a6a9a0ccf704e7f4b537"),
	"topicsList" : {
		"fromDate" : "26-05-2022",
		"fromDateString" : 20220526,
		"toDate" : "29-05-2022",
		"toDateString" : 20220529,
		"topicId" : "62860d15ae6cd23352fa3684",
		"topicName" : "1"
	},
	"userId" : ObjectId("6069a5daa0ccf704e7319d16")
}
{
	"_id" : "62860d15ae6cd23352fa3683",
	"chapterName" : "chap1",
	"groupId" : ObjectId("5f06cca74e51ba15f5167b86"),
	"teamId" : ObjectId("6069a6a9a0ccf704e7f4b537"),
	"topicsList" : {
		"fromDate" : "01-06-2022",
		"fromDateString" : 20220601,
		"toDate" : "03-06-2022",
		"toDateString" : 20220603,
		"topicId" : "62860d15ae6cd23352fa3685",
		"topicName" : "2"
	},
	"userId" : ObjectId("6069a5daa0ccf704e7319d16")
}
{
	"_id" : "62860d15ae6cd23352fa3686",
	"chapterName" : "chap2",
	"groupId" : ObjectId("5f06cca74e51ba15f5167b86"),
	"teamId" : ObjectId("6069a6a9a0ccf704e7f4b537"),
	"topicsList" : {
		"fromDate" : "02-07-2022",
		"fromDateString" : 20220702,
		"toDate" : "04-07-2022",
		"toDateString" : 20220704,
		"topicId" : "62860d15ae6cd23352fa3687",
		"topicName" : "1"
	},
	"userId" : ObjectId("6069a5daa0ccf704e7319d16")
}

any ways to get only one document in this way

{
	"_id" : "62860d15ae6cd23352fa3683",
	"chapterName" : "chap1",
	"groupId" : ObjectId("5f06cca74e51ba15f5167b86"),
	"teamId" : ObjectId("6069a6a9a0ccf704e7f4b537"),
	"topicsList" : {
		"fromDate" : "26-05-2022",
		"fromDateString" : 20220526,
		"toDate" : "29-05-2022",
		"toDateString" : 20220529,
		"topicId" : "62860d15ae6cd23352fa3684",
		"topicName" : "1"
	},
	"userId" : ObjectId("6069a5daa0ccf704e7319d16")
}

Hello @Prathamesh_N ,

It has been long since you have posted this and I think you might have found your solution. If not then you can use limit at the end of your query.

Below will be your updated Query

db.VW_SYLLABUS_PLAN_DB.find({$expr: {$and: [{$gte: [“topicsList.fromDateString”, 20220526]}, {$lte: [20220526,“topicsList.toDateString”]}]}}).pretty().limit(1)

Regards,
Tarun

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.