Query mongoDB for a date between two dates misses some documents
up vote
1
down vote
favorite
I have the following document in a collection name chats
:
"_id" : 25281,
"conversation" : [
"time" : "1970-01-18T20:16:28.988Z"
,
"time" : "2018-11-09T18:43:09.297Z"
],
For some reason this specific document is not returning in the below query, although similar documents are returning as expected.
Here is the query:
db.getCollection('chats').find("conversation.1.time":
"$gte": ISODate("2018-11-09T00:00:00.000Z"),
"$lt": ISODate("2018-11-10T00:00:00.000Z")
)
mongodb mongodb-query
add a comment |
up vote
1
down vote
favorite
I have the following document in a collection name chats
:
"_id" : 25281,
"conversation" : [
"time" : "1970-01-18T20:16:28.988Z"
,
"time" : "2018-11-09T18:43:09.297Z"
],
For some reason this specific document is not returning in the below query, although similar documents are returning as expected.
Here is the query:
db.getCollection('chats').find("conversation.1.time":
"$gte": ISODate("2018-11-09T00:00:00.000Z"),
"$lt": ISODate("2018-11-10T00:00:00.000Z")
)
mongodb mongodb-query
What's the type oftime
in your document ? Is it string or ISODate ?
– mickl
Nov 11 at 15:17
@mickl Its a string in the DB. The query is done via roboMongo Console.
– TBE
Nov 11 at 15:26
Mongo is typeless Schemeless DB isn't it?
– TBE
Nov 11 at 15:26
yes it's schemaless but types matter, check my answer
– mickl
Nov 11 at 15:30
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have the following document in a collection name chats
:
"_id" : 25281,
"conversation" : [
"time" : "1970-01-18T20:16:28.988Z"
,
"time" : "2018-11-09T18:43:09.297Z"
],
For some reason this specific document is not returning in the below query, although similar documents are returning as expected.
Here is the query:
db.getCollection('chats').find("conversation.1.time":
"$gte": ISODate("2018-11-09T00:00:00.000Z"),
"$lt": ISODate("2018-11-10T00:00:00.000Z")
)
mongodb mongodb-query
I have the following document in a collection name chats
:
"_id" : 25281,
"conversation" : [
"time" : "1970-01-18T20:16:28.988Z"
,
"time" : "2018-11-09T18:43:09.297Z"
],
For some reason this specific document is not returning in the below query, although similar documents are returning as expected.
Here is the query:
db.getCollection('chats').find("conversation.1.time":
"$gte": ISODate("2018-11-09T00:00:00.000Z"),
"$lt": ISODate("2018-11-10T00:00:00.000Z")
)
mongodb mongodb-query
mongodb mongodb-query
asked Nov 11 at 15:13
TBE
293220
293220
What's the type oftime
in your document ? Is it string or ISODate ?
– mickl
Nov 11 at 15:17
@mickl Its a string in the DB. The query is done via roboMongo Console.
– TBE
Nov 11 at 15:26
Mongo is typeless Schemeless DB isn't it?
– TBE
Nov 11 at 15:26
yes it's schemaless but types matter, check my answer
– mickl
Nov 11 at 15:30
add a comment |
What's the type oftime
in your document ? Is it string or ISODate ?
– mickl
Nov 11 at 15:17
@mickl Its a string in the DB. The query is done via roboMongo Console.
– TBE
Nov 11 at 15:26
Mongo is typeless Schemeless DB isn't it?
– TBE
Nov 11 at 15:26
yes it's schemaless but types matter, check my answer
– mickl
Nov 11 at 15:30
What's the type of
time
in your document ? Is it string or ISODate ?– mickl
Nov 11 at 15:17
What's the type of
time
in your document ? Is it string or ISODate ?– mickl
Nov 11 at 15:17
@mickl Its a string in the DB. The query is done via roboMongo Console.
– TBE
Nov 11 at 15:26
@mickl Its a string in the DB. The query is done via roboMongo Console.
– TBE
Nov 11 at 15:26
Mongo is typeless Schemeless DB isn't it?
– TBE
Nov 11 at 15:26
Mongo is typeless Schemeless DB isn't it?
– TBE
Nov 11 at 15:26
yes it's schemaless but types matter, check my answer
– mickl
Nov 11 at 15:30
yes it's schemaless but types matter, check my answer
– mickl
Nov 11 at 15:30
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
This document is not matched because there's a mismatch between ISODate
specified in your query and string
in your data model. MongoDB checks types before values so that's why you're getting no values. From docs
For most data types, however, comparison operators only perform comparisons on documents where the BSON type of the target field matches the type of the query operand.
There are three ways to fix that. You can change the type in your query:
db.getCollection('chats').find("conversation.1.time":
"$gte": "2018-11-09T00:00:00.000Z",
"$lt": "2018-11-10T00:00:00.000Z"
)
or you need to convert the data in chats
:
"_id" : 25281,
"conversation" : [
"time" : ISODate("1970-01-18T20:16:28.988Z")
,
"time" : ISODate("2018-11-09T18:43:09.297Z")
],
Alternatively you can take a look at $toDate operator in Aggregation Framework (introduced in MongoDB 4.0):
db.getCollection('chats').aggregate([
$addFields:
value: $arrayElemAt: [ "$conversation", 1 ]
,
$match:
$expr:
$and: [
$gte: [ $toDate: "$value.time" , ISODate("2018-11-09T00:00:00.000Z") ] ,
$lt: [ $toDate: "$value.time" , ISODate("2018-11-10T00:00:00.000Z") ] ,
]
])
Thanks! Is there a query i can do in order to go over all thetime
fields in allconversation
arrays in allchats
documents and update them toISODate("ORIGINAL_STRING_HERE")
– TBE
Nov 11 at 15:53
1
You can use $type operator to query such documents (docs.mongodb.com/manual/reference/operator/query/type)
– mickl
Nov 11 at 15:55
From now and on all of them will always be Date types. However the current state of the DB is that some are dates and some aren't. That is why i want to fix them all to be dates. If there isn't any query that will know how to convert them all, i'll have to write a script that does
– TBE
Nov 11 at 15:59
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
This document is not matched because there's a mismatch between ISODate
specified in your query and string
in your data model. MongoDB checks types before values so that's why you're getting no values. From docs
For most data types, however, comparison operators only perform comparisons on documents where the BSON type of the target field matches the type of the query operand.
There are three ways to fix that. You can change the type in your query:
db.getCollection('chats').find("conversation.1.time":
"$gte": "2018-11-09T00:00:00.000Z",
"$lt": "2018-11-10T00:00:00.000Z"
)
or you need to convert the data in chats
:
"_id" : 25281,
"conversation" : [
"time" : ISODate("1970-01-18T20:16:28.988Z")
,
"time" : ISODate("2018-11-09T18:43:09.297Z")
],
Alternatively you can take a look at $toDate operator in Aggregation Framework (introduced in MongoDB 4.0):
db.getCollection('chats').aggregate([
$addFields:
value: $arrayElemAt: [ "$conversation", 1 ]
,
$match:
$expr:
$and: [
$gte: [ $toDate: "$value.time" , ISODate("2018-11-09T00:00:00.000Z") ] ,
$lt: [ $toDate: "$value.time" , ISODate("2018-11-10T00:00:00.000Z") ] ,
]
])
Thanks! Is there a query i can do in order to go over all thetime
fields in allconversation
arrays in allchats
documents and update them toISODate("ORIGINAL_STRING_HERE")
– TBE
Nov 11 at 15:53
1
You can use $type operator to query such documents (docs.mongodb.com/manual/reference/operator/query/type)
– mickl
Nov 11 at 15:55
From now and on all of them will always be Date types. However the current state of the DB is that some are dates and some aren't. That is why i want to fix them all to be dates. If there isn't any query that will know how to convert them all, i'll have to write a script that does
– TBE
Nov 11 at 15:59
add a comment |
up vote
1
down vote
accepted
This document is not matched because there's a mismatch between ISODate
specified in your query and string
in your data model. MongoDB checks types before values so that's why you're getting no values. From docs
For most data types, however, comparison operators only perform comparisons on documents where the BSON type of the target field matches the type of the query operand.
There are three ways to fix that. You can change the type in your query:
db.getCollection('chats').find("conversation.1.time":
"$gte": "2018-11-09T00:00:00.000Z",
"$lt": "2018-11-10T00:00:00.000Z"
)
or you need to convert the data in chats
:
"_id" : 25281,
"conversation" : [
"time" : ISODate("1970-01-18T20:16:28.988Z")
,
"time" : ISODate("2018-11-09T18:43:09.297Z")
],
Alternatively you can take a look at $toDate operator in Aggregation Framework (introduced in MongoDB 4.0):
db.getCollection('chats').aggregate([
$addFields:
value: $arrayElemAt: [ "$conversation", 1 ]
,
$match:
$expr:
$and: [
$gte: [ $toDate: "$value.time" , ISODate("2018-11-09T00:00:00.000Z") ] ,
$lt: [ $toDate: "$value.time" , ISODate("2018-11-10T00:00:00.000Z") ] ,
]
])
Thanks! Is there a query i can do in order to go over all thetime
fields in allconversation
arrays in allchats
documents and update them toISODate("ORIGINAL_STRING_HERE")
– TBE
Nov 11 at 15:53
1
You can use $type operator to query such documents (docs.mongodb.com/manual/reference/operator/query/type)
– mickl
Nov 11 at 15:55
From now and on all of them will always be Date types. However the current state of the DB is that some are dates and some aren't. That is why i want to fix them all to be dates. If there isn't any query that will know how to convert them all, i'll have to write a script that does
– TBE
Nov 11 at 15:59
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
This document is not matched because there's a mismatch between ISODate
specified in your query and string
in your data model. MongoDB checks types before values so that's why you're getting no values. From docs
For most data types, however, comparison operators only perform comparisons on documents where the BSON type of the target field matches the type of the query operand.
There are three ways to fix that. You can change the type in your query:
db.getCollection('chats').find("conversation.1.time":
"$gte": "2018-11-09T00:00:00.000Z",
"$lt": "2018-11-10T00:00:00.000Z"
)
or you need to convert the data in chats
:
"_id" : 25281,
"conversation" : [
"time" : ISODate("1970-01-18T20:16:28.988Z")
,
"time" : ISODate("2018-11-09T18:43:09.297Z")
],
Alternatively you can take a look at $toDate operator in Aggregation Framework (introduced in MongoDB 4.0):
db.getCollection('chats').aggregate([
$addFields:
value: $arrayElemAt: [ "$conversation", 1 ]
,
$match:
$expr:
$and: [
$gte: [ $toDate: "$value.time" , ISODate("2018-11-09T00:00:00.000Z") ] ,
$lt: [ $toDate: "$value.time" , ISODate("2018-11-10T00:00:00.000Z") ] ,
]
])
This document is not matched because there's a mismatch between ISODate
specified in your query and string
in your data model. MongoDB checks types before values so that's why you're getting no values. From docs
For most data types, however, comparison operators only perform comparisons on documents where the BSON type of the target field matches the type of the query operand.
There are three ways to fix that. You can change the type in your query:
db.getCollection('chats').find("conversation.1.time":
"$gte": "2018-11-09T00:00:00.000Z",
"$lt": "2018-11-10T00:00:00.000Z"
)
or you need to convert the data in chats
:
"_id" : 25281,
"conversation" : [
"time" : ISODate("1970-01-18T20:16:28.988Z")
,
"time" : ISODate("2018-11-09T18:43:09.297Z")
],
Alternatively you can take a look at $toDate operator in Aggregation Framework (introduced in MongoDB 4.0):
db.getCollection('chats').aggregate([
$addFields:
value: $arrayElemAt: [ "$conversation", 1 ]
,
$match:
$expr:
$and: [
$gte: [ $toDate: "$value.time" , ISODate("2018-11-09T00:00:00.000Z") ] ,
$lt: [ $toDate: "$value.time" , ISODate("2018-11-10T00:00:00.000Z") ] ,
]
])
answered Nov 11 at 15:29
mickl
10.7k51535
10.7k51535
Thanks! Is there a query i can do in order to go over all thetime
fields in allconversation
arrays in allchats
documents and update them toISODate("ORIGINAL_STRING_HERE")
– TBE
Nov 11 at 15:53
1
You can use $type operator to query such documents (docs.mongodb.com/manual/reference/operator/query/type)
– mickl
Nov 11 at 15:55
From now and on all of them will always be Date types. However the current state of the DB is that some are dates and some aren't. That is why i want to fix them all to be dates. If there isn't any query that will know how to convert them all, i'll have to write a script that does
– TBE
Nov 11 at 15:59
add a comment |
Thanks! Is there a query i can do in order to go over all thetime
fields in allconversation
arrays in allchats
documents and update them toISODate("ORIGINAL_STRING_HERE")
– TBE
Nov 11 at 15:53
1
You can use $type operator to query such documents (docs.mongodb.com/manual/reference/operator/query/type)
– mickl
Nov 11 at 15:55
From now and on all of them will always be Date types. However the current state of the DB is that some are dates and some aren't. That is why i want to fix them all to be dates. If there isn't any query that will know how to convert them all, i'll have to write a script that does
– TBE
Nov 11 at 15:59
Thanks! Is there a query i can do in order to go over all the
time
fields in all conversation
arrays in all chats
documents and update them to ISODate("ORIGINAL_STRING_HERE")
– TBE
Nov 11 at 15:53
Thanks! Is there a query i can do in order to go over all the
time
fields in all conversation
arrays in all chats
documents and update them to ISODate("ORIGINAL_STRING_HERE")
– TBE
Nov 11 at 15:53
1
1
You can use $type operator to query such documents (docs.mongodb.com/manual/reference/operator/query/type)
– mickl
Nov 11 at 15:55
You can use $type operator to query such documents (docs.mongodb.com/manual/reference/operator/query/type)
– mickl
Nov 11 at 15:55
From now and on all of them will always be Date types. However the current state of the DB is that some are dates and some aren't. That is why i want to fix them all to be dates. If there isn't any query that will know how to convert them all, i'll have to write a script that does
– TBE
Nov 11 at 15:59
From now and on all of them will always be Date types. However the current state of the DB is that some are dates and some aren't. That is why i want to fix them all to be dates. If there isn't any query that will know how to convert them all, i'll have to write a script that does
– TBE
Nov 11 at 15:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250093%2fquery-mongodb-for-a-date-between-two-dates-misses-some-documents%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What's the type of
time
in your document ? Is it string or ISODate ?– mickl
Nov 11 at 15:17
@mickl Its a string in the DB. The query is done via roboMongo Console.
– TBE
Nov 11 at 15:26
Mongo is typeless Schemeless DB isn't it?
– TBE
Nov 11 at 15:26
yes it's schemaless but types matter, check my answer
– mickl
Nov 11 at 15:30