_Christian
(Christian)
November 21, 2023, 6:57pm
1
I have a collection of documents in my DB that look roughly like this:
{
"foo": {
"bar": new ISODate("2020-05-18T14:10:30Z")
}
}
I want to update all the documents that have a date there by converting the date to a number (milliseconds since epoch).
Here is what I tried:
db.collection.updateMany(
{ "foo.bar": { $type: "date" } },
[
{ $set: { "foo.bar": { $toLong: "$foo.bar" }}}
]
)
Alas, that doesn’t seem to do anything. What am I doing wrong?
(There are many more properties on both levels, in case that makes a difference…)
Jason_Tran
(Jason Tran)
November 22, 2023, 12:11am
2
Hi @_Christian ,
Seemed to have work for me for the following document:
test>db.coll2.find({},{_id:0})
[
{ foo: { bar: ISODate("2020-05-18T14:10:30.000Z") } }
]
test> db.coll2.updateMany(
{ "foo.bar": { $type: "date" } },
[
{ $set: { "foo.bar": { $toLong: "$foo.bar" }}}
]
)
Output after the update:
test> db.coll2.find({},{_id:0})
[
{
foo: { bar: Long("1589811030000") }
}
]
What was the output from running the updateMany()
? Did you receive any value > 0 for matchedCount
and/or modifiedCount
?
Can you also provide sample document(s) that you expect to be updated as well as the MongoDB version you’re running?
I could be missing something obvious but good to get those pieces of information as well
Regards,
Jason
_Christian
(Christian)
November 22, 2023, 10:27am
3
Hi Jason,
Thanks for having a look and trying this for me.
Turns out that for that one statement, I had literally written “collection
” instead of the collection’s actual name.
I’m using IntelliJ to run these from its DB Console window; sadly it just reports
[date-time] completed in xyz ms
in either case. No mention of affected documents. Which is a bit unhelpful, really…
But it’s all working now; types converted successfully.
1 Like
system
(system)
Closed
November 27, 2023, 10:27am
4
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.