0

I have a client facing application with a function that takes in 2 arguments as strings, arg1 is the collection, arg 2 is the function, arg 2 is the hash of the object

so in Java I have

foo(String collection, String object):

/*and I have my db object from the mongoDB driver the collection I want to insert into is "users" */

MongoClient mongoClient = new MongoClient( "localhost" );
DB db = mongoClient.getDB("mydb");

now here is where I am having trouble

db.runCommand({insert : collection (? can i do this),
               ????}) <- I dont know how to right this and append the object

I did a bunch of searching before hand and a lot of the examples I found already had a predefined collection but I need to abstract this.

Any help would be extremely useful, thank you.

UPDATE:

I am not looking for coll.find() java method. I want to visualize someones mongoDB data with a better output than what the shell provides. So I am looking for a very general db.runcommand(string) that can take in an insert/find/findone() whatever is passed in as a string. I can get collection names using runcommand so I understand it on a basic level, but cannot apply specific commands to any user defined collection.

2 Answers2

1

Sample:

DBCollection coll = db.getCollection("collection");

/* {
   "name" : "MongoDB",
   "type" : "database",
   "count" : 1,
   "info" : {
               x : 203,
               y : 102
             }
} */

BasicDBObject doc = new BasicDBObject("name", "MongoDB").
                              append("type", "database").
                              append("count", 1).
                              append("info", new BasicDBObject("x", 203).append("y", 102));

coll.insert(doc);
mert
  • 920
  • 3
  • 15
  • 24
  • Is the document structure section supposed to be in a comment? Because if you copied that code into a class it wouldn't compile. – Trisha Jul 18 '13 at 08:46
  • @mert I meant in the case that you don't know if the user is going to pass in collection.insert() or collection.find() or collection.insert().find().aggregate() whatever. What if you didn't even know what collection? Is there a way to mimic raw mongoDB without making my own mapping? thank you – user2592882 Jul 26 '13 at 23:18
1

It sounds almost like you're trying to write your own driver - working out what command the user wants to do and then turning it into BSON to send to MongoDB is exactly what the MongoDB Java driver does.

You can run arbitrary commands using the Java driver, using db.command(...). You'll have to create a DBObject that represents the command you want to run.

With regards to your comment "examples I found already had a predefined collection but I need to abstract this.", @mert was correct in that you can get the Collection you need using:

DBCollection coll = db.getCollection(collection);

Where collection is the String variable declared in your foo method.

However, I'm not sure about the security of the functionality you wish to provide - you're basically providing a tool to let a user run any arbitrary commands against your database, and although MongoDB is a NoSQL Database opening it up like this exposes similar vulnerabilities to a SQL Injection attack. What exactly is your use case? Do you really want to circumvent and re-invent the driver to allow your users to do anything they want on the database? Drivers are written with a strict API for two reasons 1) to help the developer and 2) to prevent people doing incorrect or nasty things to the database.

Trisha
  • 3,891
  • 1
  • 25
  • 39