简体   繁体   English

MongoDB Java驱动程序:如何从Java驱动程序插入任何集合

[英]MongoDB Java Driver: How to insert into any collection from Java Driver

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 我有一个面向应用程序的客户端,其函数接收2个参数作为字符串,arg1是集合,arg 2是函数,arg 2是对象的散列

so in Java I have 所以在Java中我有

foo(String collection, String object):

/*and I have my db object from the mongoDB driver the collection I want to insert into is "users" */ / *我从mongoDB驱动程序获取我的db对象我要插入的集合是“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. 我不是在寻找coll.find()java方法。 I want to visualize someones mongoDB data with a better output than what the shell provides. 我希望可视化某些人的mongoDB数据,其输出比shell提供的更好。 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. 所以我正在寻找一个非常通用的db.runco​​mmand(string),它可以接受insert / find / findone()以字符串形式传入的内容。 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. 我可以使用runco​​mmand获取集合名称,所以我在基本级别上了解它,但不能将特定命令应用于任何用户定义的集合。

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);

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. 这听起来几乎就像你正在尝试编写自己的驱动程序 - 确定用户想要做什么命令然后将其转换为BSON以发送到MongoDB正是MongoDB Java驱动程序所做的。

You can run arbitrary commands using the Java driver, using db.command(...) . 您可以使用db.command(...)使用Java驱动程序运行任意命令。 You'll have to create a DBObject that represents the command you want to run. 您必须创建一个表示要运行的命令的DBObject。

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: 关于你的评论“我发现已有预定义集合的例子,但我需要抽象它。”,@ mert是正确的,你可以使用以下方法获得你需要的集合:

DBCollection coll = db.getCollection(collection);

Where collection is the String variable declared in your foo method. 其中collection是在foo方法中声明的String变量。

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. 但是,我不确定您希望提供的功能的安全性 - 您基本上提供了一个工具,让用户对您的数据库运行任意命令,尽管MongoDB是一个NoSQL数据库打开它像这样暴露SQL注入攻击的类似漏洞。 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. 驱动程序使用严格的API编写,原因有两个:1)帮助开发人员; 2)防止人们对数据库做错误或讨厌的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM