nightroman / Mdbc

MongoDB Cmdlets for PowerShell
Apache License 2.0
141 stars 16 forks source link

How to call database methods such as db.getCollectionInfos()? #49

Closed awsles closed 3 years ago

awsles commented 3 years ago

How can I call a database method such as db.getCollectionInfos() using Invoke-MdbcCommand?

I need to retrieve the validator settings for a given collection and this is the only way to do so (apparently). But in general, it would be good to know the correct syntax for calling the MongoDB database methods. An example here and in the help would be great.

If there is no way to invoke a database method, then please add Invoke-MdbcMethod to the wish list! Thank you.

nightroman commented 3 years ago

Invoke-MdbcCommand is exactly what you look for.

Example for your question:

Connect-Mdbc . test
Invoke-MdbcCommand '{listCollections: { resource: { db: "test", collection: "" }, actions: [ "listCollections" ] }}'

See help for Invoke-MdbcCommand and a few examples there.

But Mdbc docs do not provide syntax of MongoDB commands. There is no need to duplicate this huge amount of information. See MongoDB http://www.mongodb.org for details.

If you take a closer look at the link that you provided, you will find the syntax for the above example :)

awsles commented 3 years ago

The above sample only produces the following output:

Name                           Value                                                                                                          
----                           -----                                                                                                          
cursor                         {id, ns, firstBatch}                                                                                           
ok                             1                                                                                                              
$clusterTime                   {clusterTime, signature}                                                                                       
operationTime                  6900489732736679937                                                                                            

I need to find a way to list the collections with the validators. Get-mdbcCollection doesn't return this. Part of my confusion is how to translate examples provided in MongoDB documentation to the JSON syntax that you have defined for Invoke-MdbcCommand. It's not obvious to me. For example, the MongoDB documentation provides an example such as:

{ resource: { db: "test", collection: "" }, actions: [ "listCollections" ] }

I've had no luck enumerating collections using the above. Any thoughts? Much apprecaited.

nightroman commented 3 years ago

@lesterw1 I do not really know, too. If I knew I'd tell you right away. I've just tried to save the result to $r and then $r.cursor.firstBatch gets my collections. Dig into MongoDB and C# driver docs for more details or ask questions at their forum or StackOverflow.

gjh71 commented 3 years ago

How can I call a database method such as db.getCollectionInfos() using Invoke-MdbcCommand?

I need to retrieve the validator settings for a given collection and this is the only way to do so (apparently). But in general, it would be good to know the correct syntax for calling the MongoDB database methods. An example here and in the help would be great.

If there is no way to invoke a database method, then please add Invoke-MdbcMethod to the wish list! Thank you.

I'd say the addition of Invoke-MdbcMethod would be rather elegant. Myself I'm looking for the number of connections: 'db.serverStatus().connections'