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")

)









share|improve this question





















  • 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














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")

)









share|improve this question





















  • 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












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")

)









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 15:13









TBE

293220




293220











  • 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
















  • 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















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












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") ] ,
]



])





share|improve this answer




















  • 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




    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











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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") ] ,
]



])





share|improve this answer




















  • 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




    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















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") ] ,
]



])





share|improve this answer




















  • 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




    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













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") ] ,
]



])





share|improve this answer












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") ] ,
]



])






share|improve this answer












share|improve this answer



share|improve this answer










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 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




    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







  • 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


















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto