0

when i see the this answer// convert this type of string date And I followed it this way.

20170721123840+0900

var a = new Date({$concat: [ "2017"," ",
                                 "11"," ",
                                 "11"," ",
                                 "11",":",
                                 "11",":",
                                 "11"," ",
                                 "+0900" ]  } )
a

This is what I expected.

var b = new Date("2017 07 21 12:38:40 +0900")
b

ISODate("2017-07-21T03:38:40Z")

But this was the result.

ISODate("0NaN-NaN-NaNTNaN:NaN:NaNZ")

Why is this form coming out? What do you do to solve it?

sonjm
  • 15
  • 7
  • You cannot "cast" types in an aggregation pipeline, which is of course the **only** place [`$concat`](https://docs.mongodb.com/manual/reference/operator/aggregation/concat/) can actually be used ( since it is an aggregation pipeline operator ). In the context you are trying you simply use regular JavaScript expressions to "concatenate" the strings. If your actual intent to is convert existing document data from "string" to BSON Date, then instead you must "loop" the results and update, as is also pointed out on the answers to the same question you reference. – Neil Lunn Jul 27 '17 at 02:35
  • @NeilLunn Thank you for your kind reply. So, is it impossible to convert a string date to a date type in a query to search for a period? – sonjm Jul 27 '17 at 02:48
  • What is your actual question? Why do you have a "string" that is in "parts" you need to concatenate? If you have something different to do than is actually described in the linked questions and answers then [Edit your question](https://stackoverflow.com/posts/45340317/edit) to show what you really need to do. Your question makes no mention of *"query to search for a period"*. But you are perhaps asking something already answered in [Find objects between two dates MongoDB](https://stackoverflow.com/q/2943222/2313887) – Neil Lunn Jul 27 '17 at 02:52

0 Answers0