0

I am setting the mongo logger as below

<logger name="org.mongodb" level="debug"/>

When I run the query using java driver in Micronuat application

DEBUG org.mongodb.driver.protocol.command - Sending command '{"aggregate": "product.category", "pipeline": [{"$match": {"$and": [{"_id": {"$oid": "60c4aed3afb396342bf90802"}}, {"subCategory._id": {"$oid": "60cc5dd52b5cf415ded7de5d"}}]}}, {"$group": {"_id": 1, "n": {"$sum": 1}}}], "cursor": {}, "$db": "FeteBird-Product", "lsid": {"id": {"$binary": {"base64": "9UGXhsvnSZ+FXRCDwsiyWA==", "subType": "04"}}}}'

But if I copy this command and try to run in mongoshell it won't work, how can I show what exact query it is generating. So that I can run that query and see the output on console

San Jaisy
  • 15,327
  • 34
  • 171
  • 290
  • possible duplicate of https://stackoverflow.com/questions/48326299/how-can-i-log-actual-queries-to-mongodb-with-mongo-java-driver – Javier Toja Jun 18 '21 at 14:37
  • What you are seeing is a full representation of the query, the first part with the aggregate and the pipeline is the query the rest is just additional info about the db where you executed the query, check the mongodb-shell sintax but all the information is there – Javier Toja Jun 18 '21 at 14:41
  • How can I execute that command – San Jaisy Jun 18 '21 at 14:57

1 Answers1

0

Try running your command using runCommand

mongo shell> use database_name
mongo shell> db.runCommand({"aggregate": "product.category", "pipeline": [{"$match": {"$and": [{"_id": {"$oid": "60c4aed3afb396342bf90802"}}, {"subCategory._id": {"$oid": "60cc5dd52b5cf415ded7de5d"}}]}}, {"$group": {"_id": 1, "n": {"$sum": 1}}}], "cursor": {}, "$db": "FeteBird-Product", "lsid": {"id": {"$binary": {"base64": "9UGXhsvnSZ+FXRCDwsiyWA==", "subType": "04"}}}})
geobreze
  • 2,274
  • 1
  • 10
  • 15
  • Any idea a how can I convert 60c4aed3afb396342bf90802 to BSON, getting an exception while running the above command – San Jaisy Jun 18 '21 at 15:25
  • com.mongodb.MongoCommandException: Command failed with error 14 (TypeMismatch): 'BSON field 'OperationSessionInfo.lsid.id' is the wrong type 'object', expected type 'binData'' on server localhost:27017. The full response is {"ok": 0.0, "errmsg": "BSON field 'OperationSessionInfo.lsid.id' is the wrong type 'object', expected type 'binData'", "code": 14, "codeName": "TypeMismatch"} – San Jaisy Jun 18 '21 at 15:28
  • Actually error says to fix `OperationSessionInfo.lsid.id` field, not `subCategory._id` field. `$oid` is a way to build `ObjectId` instance – geobreze Jun 18 '21 at 15:47
  • db.runCommand( {"aggregate": "product.category", "pipeline": [ {"$match": {"$and": [{"_id": "60c4aed3afb396342bf90802"}, {"subCategory._id": "60cc5dd52b5cf415ded7de5d"}]} } ], "cursor": {}, "$db": "FeteBird-Product"} ) this works but lots of modification – San Jaisy Jun 18 '21 at 15:53
  • You can try replacing `"lsid": {"id": {"$binary": {"base64": "9UGXhsvnSZ+FXRCDwsiyWA==", "subType": "04"}}}` with `"lsid: {"id": BinData(4, "9UGXhsvnSZ+FXRCDwsiyWA==")}` – geobreze Jun 18 '21 at 15:57
  • unknown operator: $oid on running the command – San Jaisy Jun 18 '21 at 16:00
  • What version of monogdb are you using? Try to replace every `{"$oid": "something"}` with `ObjectId("something")` – geobreze Jun 18 '21 at 16:03
  • MongoDB shell version v4.2.0. I did getting unknown operator: $oid on running the command – San Jaisy Jun 18 '21 at 16:05
  • For mongo shell mode try using mongo shell representation of strict commands from https://docs.mongodb.com/v4.2/reference/mongodb-extended-json-v1/ – geobreze Jun 18 '21 at 16:12