Skip to content

Commit 50713d9

Browse files
committed
DOCS-7898: mongoexport and strict json fmt date
1 parent 232efb4 commit 50713d9

File tree

4 files changed

+58
-31
lines changed

4 files changed

+58
-31
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
You must enclose the query in single quotes (e.g. ``'``) to ensure that it does
1+
You must enclose the query document in single quotes (``'{ ... }'``) to ensure that it does
22
not interact with your shell environment.

source/includes/options-mongoexport.yaml

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ aliases: -q
228228
directive: option
229229
args: <JSON>
230230
description: |
231-
Provides a :term:`JSON document` as a query that optionally limits
232-
the documents returned in the export. Specify JSON in :doc:`strict
231+
Provides a query as a :term:`JSON document` (enclosed in quotes) to
232+
return matching documents in the export. Specify JSON in :doc:`strict
233233
format </reference/mongodb-extended-json>`.
234234
235235
.. include:: /includes/fact-quote-command-line-query.rst
@@ -239,26 +239,29 @@ description: |
239239
240240
.. code:: json
241241
242-
{ "_id" : ObjectId("51f0188846a64a1ed98fde7c"), "a" : 1 }
243-
{ "_id" : ObjectId("520e61b0c6646578e3661b59"), "a" : 1, "b" : 2 }
244-
{ "_id" : ObjectId("520e642bb7fa4ea22d6b1871"), "a" : 2, "b" : 3, "c" : 5 }
245-
{ "_id" : ObjectId("520e6431b7fa4ea22d6b1872"), "a" : 3, "b" : 3, "c" : 6 }
246-
{ "_id" : ObjectId("520e6445b7fa4ea22d6b1873"), "a" : 5, "b" : 6, "c" : 8 }
242+
{ "_id" : ObjectId("51f0188846a64a1ed98fde7c"), "a" : 1, "date" : ISODate("1960-05-01T00:00:00Z") }
243+
{ "_id" : ObjectId("520e61b0c6646578e3661b59"), "a" : 1, "b" : 2, "date" : ISODate("1970-05-01T00:00:00Z") }
244+
{ "_id" : ObjectId("520e642bb7fa4ea22d6b1871"), "a" : 2, "b" : 3, "c" : 5, "date" : ISODate("2010-05-01T00:00:00Z") }
245+
{ "_id" : ObjectId("520e6431b7fa4ea22d6b1872"), "a" : 3, "b" : 3, "c" : 6, "date" : ISODate("2015-05-02T00:00:00Z") }
246+
{ "_id" : ObjectId("520e6445b7fa4ea22d6b1873"), "a" : 5, "b" : 6, "c" : 8, "date" : ISODate("2018-03-01T00:00:00Z") }
247+
{ "_id" : ObjectId("5cd0de910dbce4346295ae28"), "a" : 15, "b" : 5, "date" : ISODate("2015-03-01T00:00:00Z") }
247248
248249
The following :binary:`~bin.mongoexport` uses the :option:`-q` option to
249250
export only the documents with the field ``a`` greater than or equal to
250-
(:query:`$gte`) to ``3``:
251+
(:query:`$gte`) to ``3`` and the field ``date`` less than
252+
``ISODate("2016-01-01T00:00:00Z")`` (using the :ref:`strict format
253+
for dates { "$date": "YYYY-MM-DDTHH:mm:ss.mmm\<offset\>"} <extended-json-date>`):
251254
252255
.. code:: bash
253256
254-
mongoexport -d test -c records -q '{ a: { $gte: 3 } }' --out exportdir/myRecords.json
257+
mongoexport -d test -c records -q '{ a: { $gte: 3 }, date: { $lt: { "$date": "2016-01-01T00:00:00.000Z" } } }' --out exportdir/myRecords.json
255258
256259
The resulting file contains the following documents:
257260
258261
.. code:: json
259262
260-
{ "_id" : { "$oid" : "520e6431b7fa4ea22d6b1872" }, "a" : 3, "b" : 3, "c" : 6 }
261-
{ "_id" : { "$oid" : "520e6445b7fa4ea22d6b1873" }, "a" : 5, "b" : 6, "c" : 8 }
263+
{"_id":{"$oid":"520e6431b7fa4ea22d6b1872"},"a":3.0,"b":3.0,"c":6.0,"date":{"$date":"2015-05-02T00:00:00Z"}}
264+
{"_id":{"$oid":"5cd0de910dbce4346295ae28"},"a":15.0,"b":5.0,"date":{"$date":"2015-03-01T00:00:00Z"}}
262265
263266
You can sort the results with the :option:`--sort` option to
264267
:binary:`~bin.mongoexport`.

source/reference/mongodb-extended-json.txt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ Binary
109109
*Strict mode* it is a hexadecimal string, and in *Shell mode* it is an integer.
110110
See the extended bson documentation. http://bsonspec.org/spec.html
111111

112+
.. _extended-json-date:
113+
112114
Date
113115
~~~~
114116

@@ -137,15 +139,6 @@ Date
137139
In *Strict mode*, ``<date>`` is an ISO-8601 date format with a mandatory time
138140
zone field following the template ``YYYY-MM-DDTHH:mm:ss.mmm<+/-Offset>``.
139141

140-
The MongoDB JSON parser currently does not support loading ISO-8601 strings
141-
representing dates prior to the :term:`Unix epoch`. When formatting
142-
pre-epoch dates and dates past what your system's ``time_t`` type can hold,
143-
the following format is used:
144-
145-
.. code-block:: none
146-
147-
{ "$date" : { "$numberLong" : "<dateAsMilliseconds>" } }
148-
149142
In *Shell mode*, ``<date>`` is the JSON representation of a 64-bit signed
150143
integer giving the number of milliseconds since epoch UTC.
151144

source/reference/program/mongoexport.txt

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,29 +252,60 @@ writes the export to the ``contacts.json`` file in :term:`JSON` format.
252252
Export from Remote Host Running with Authentication
253253
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
254254

255-
The following example exports the ``contacts`` collection from the
256-
``marketing`` database, which requires authentication.
255+
The following example exports the ``contacts`` collection in the
256+
``marketing`` database from a remote MongoDB instance that requires
257+
authentication.
257258

258-
This data resides on the MongoDB instance located on the host
259-
``mongodb1.example.net`` running on port ``37017``, which requires the username
260-
``user`` and the password ``pass``.
259+
Specify the:
260+
261+
- :option:`--host <mongoexport --host>`
262+
263+
- :option:`--port <mongoexport --port>`
264+
265+
- :option:`--username <mongoexport --username>`
266+
267+
- :option:`--authenticationDatabase <mongoexport --authenticationDatabase>`
268+
269+
- :option:`--collection <mongoexport --collection>`
270+
271+
- :option:`--db <mongoexport --db>`
272+
273+
- :option:`--out <mongoexport --out>`
274+
275+
.. tip::
276+
277+
Omit the :option:`--password <mongoexport --password>` option to
278+
have ``mongoexport`` prompt for the password:
279+
280+
.. code-block:: sh
281+
282+
mongoexport --host mongodb1.example.net --port 27017 --username someUser --authenticationDatabase admin
283+
284+
Alternatively, you use the :option:`--uri <mongoexport --uri>` option to specify the host, port, username, authentication database, and db.
285+
286+
.. tip::
287+
288+
Omit the password in the URI string to have ``mongoexport`` prompt
289+
for the password:
261290

262291
.. code-block:: sh
263292

264-
mongoexport --host mongodb1.example.net --port 37017 --username user --password "pass" --collection contacts --db marketing --out mdb1-examplenet.json
293+
mongoexport --uri 'mongodb://someUser@mongodb0.example.com:27017/marketing?authsource=admin' --collection contacts --out mdb1-examplenet.json
265294

266295
Export Query Results
267296
~~~~~~~~~~~~~~~~~~~~
268297
You can export only the results of a query by supplying a query filter with
269298
the :option:`--query <mongoexport --query>` option, and limit the results to a single
270299
database using the ":option:`--db <mongoexport --db>`" option.
271300

272-
For instance, this command returns all documents in the ``sales`` database's
273-
``contacts`` collection that contain a field named ``field`` with a value
274-
of ``1``.
301+
For instance, this command returns all documents in the ``sales``
302+
database's ``contacts`` collection that contain a field named ``dept``
303+
equal to ``"ABC"`` and the field ``date`` greater than or equal to
304+
ISODate("2018-01-01") (using the :ref:`strict format for dates
305+
{ "$date": "YYYY-MM-DDTHH:mm:ss.mmm\<offset\>"} <extended-json-date>` )
275306

276307
.. code-block:: sh
277308

278-
mongoexport --db sales --collection contacts --query '{"field": 1}'
309+
mongoexport --db sales --collection contacts --query '{"dept": "ABC", date: { $gte: { "$date": "2018-01-01T00:00:00.000Z" } }}'
279310

280311
.. include:: /includes/fact-quote-command-line-query.rst

0 commit comments

Comments
 (0)