0

My documents are organized this way:

{
        "_id" : ObjectId("5ea79b5da8d460059a5f58eb"),
        "direction" : 0,
        "metrictimestamp" : ISODate("2018-02-01T02:59:55Z"),
        "odometer" : 19030291,
        "routecode" : 0,
        "speed" : 0,
        "deviceid" : 8155064,
        "vehicleid" : 34489,
        "location" : {
                "type" : "Point",
                "coordinates" : [
                        -3.878595,
                        -38.533493
                ]
        }
}

I need to "mongoexport" all the documents with tha same date, so i need a query that cuts the HH:MM:SS from the metrictime stamp field, thanks for your time!

JediJesus
  • 329
  • 1
  • 9
  • Assuming you want a CSV or JSON --> does the timestamp field matter for a query or a particular data? What's the reasoning behind having the same metric time stamp? – Nadine Rose Aug 04 '20 at 18:36
  • Are you trying to use a filter to just export all the items with a specific date? Or do you want to export the items and replace the date value from 2020-01-01T02:0259:55Z to 2020-01-01 ? – Jacob Kaniuk Aug 04 '20 at 18:37
  • Honestly, if it doesn't matter -- you can use REGEX and just change it. Export the data, delete the metric time field in your JSON or CSV using REGEX. – Nadine Rose Aug 04 '20 at 18:38
  • i need to get all the documents from a certain day to analyze them, but i cant discard the time of the day – JediJesus Aug 05 '20 at 17:06

3 Answers3

2

To only export documents on a particular day use the $gt and $lt opperators and the --query arg on mongoexport like this:

https://docs.mongodb.com/v4.2/reference/program/mongoexport/#cmdoption-mongoexport-query

mongoexport -d=dbname -c=collection -q='{ "metrictimestamp": { "$gte": { "$date": "2018-02-01T00:00:00Z" }, "$lt": { "$date": "2018-02-02T00:00:00Z" } } }'
GitGitBoom
  • 1,822
  • 4
  • 5
  • thats what i need, but i keep on getting this error: too many positional arguments: ["metrictimestamp": {$gte: { $date: "2018-02-01T00:00:00Z" }, $lt: { $date: "2018-02-02T00:00:00Z" } } }'] – JediJesus Aug 05 '20 at 17:02
  • Are you on windows? You may need to invert the quotes in the JSON. Take a look at https://stackoverflow.com/questions/7521163/what-does-too-many-positional-options-mean-when-doing-a-mongoexport – GitGitBoom Aug 05 '20 at 17:07
0

from_date = ISODate("2018-02-01T00:00:00Z") day begin

to_date = ISODate("2018-02-01T23:59:59Z") day end

$date: {"$gte": from_date, "$lt": to_date}

You can have something like this in your query to return all items between those 2 dates

Jacob Kaniuk
  • 175
  • 6
0

this works for me remoting to a docker container with mongo:4.2.6

mongoexport -h mongodb:27017 --authenticationDatabase=admin -u username -p password -d database -c collection -q {"metrictimestamp": { "$gte": { "$date": "2020-08-03T00:00:00.000Z" }, "$lt": { "$date": "2020-08-09T23:59:59.999Z" } } } --fields=somefield1,somefield2 --type=csv --out=/archive.csv
user739313
  • 61
  • 1
  • 5