0

In brief

We have query written in Python via pymongo package as below sample and we want to run the same query via mongo cli in console. How can we do that ie. to get the javascript syntax of that query?

Similar need for postgres can be archieved here

In full

Let's take below sample code

from pymongo import MongoClient
client  = MongoClient('mongodb://YOUR_HOST:27017/', connect=False)
mongodb = client['YOUR_DB']
docs = mongodb['YOUR_COLLECTION'].aggregate([
    {'$match':{
        'someField': 'someFilter',
    }},
], allowDiskUse=True)

I want to run that query, ie. the javascript syntax version, via mongo cli in console e.g. on another server that has full documents/rows.

My google search and search on our site result little helpful so I asked here.

Nam G VU
  • 33,193
  • 69
  • 233
  • 372

1 Answers1

0

Here is the Javascript Syntax of the Pymongo aggregation query for the MongoDB shell.

db.YOUR_COLLECTION.aggregate(
  [{$match: {someField: someFilter}}], 
  {allowDiskUse: true}
)
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
  • Any way to get that automatically? – Nam G VU Mar 02 '18 at 12:57
  • @NamGVU None that I know of. If you'll like to use the Javascript query for maintenance reasons e.g. your team mostly been familiar with Javascript queries. You can use `Code` objects for your queries. – Oluwafemi Sule Mar 02 '18 at 14:33
  • It's not the matter of javascript-friendly in the team. We both work with python and javascript for mongo. But on production server, we can hardly run python mongo query ie. we use mongo cli. – Nam G VU Mar 03 '18 at 02:55
  • About Code object, may you clarify that? Thank you. – Nam G VU Mar 03 '18 at 02:57