简体   繁体   English

从mongodb获取数据并插入mongodb

[英]get the data from mongodb and insert in mongodb

I am totally new to mongoDB . 我是mongoDB Currently , I am working on an application , where there is a search button which will search into the mongoDB, if value is present then it will return a JSON data .If not then I am giving a functionality through which user can add in mongoDB . 目前,我正在开发一个应用程序,其中有一个搜索按钮,它将搜索到mongoDB,如果存在值,那么它将返回一个JSON数据。如果没有,那么我将提供一个功能,用户可以通过该功能添加mongoDB。

BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put(URL_KEY, companyUrl);
NoticePay noticePay;
Document mongoDoc = collection.find(whereQuery).projection(fields(include(URL_KEY, NOTICE_PERIOD_KEY, PAY_SCALE_KEY), excludeId())).first();

Here , My query is not working properly . 在这里,我的查询无法正常工作。

{ "_id" : ObjectId("5804926140fbe41a7c5afbf0"), "companies" : { "companyName" : "ABC", "url" : "abc.com", "noticePeriod" : 50, "payScale" : "M" } }

So , How can I change my query so that I will be able to search it using this json . 那么,我如何更改我的查询,以便我能够使用此json搜索它。 Previously , It was working with following form of json 以前,它正在使用以下形式的json

{ "_id" : ObjectId("5801dcf110227b0bcd178292"), "companyName" : "ABC ", "url" : "Abc.com", "noticekPeriod" : 60, "payScale" : "L" }

Can any one help me to solve this issue ? 任何人都可以帮我解决这个问题吗? And also how can I insert a document in the 2nd form in mongodb using java ? 另外我如何使用java在mongodb中以第二种形式插入文档? Thanks in advance. 提前致谢。

How can I change my query so that I will be able to search it using this json { "_id" : ObjectId("5804926140fbe41a7c5afbf0"), "companies" : { "companyName" : "ABC", "url" : "abc.com", "noticePeriod" : 50, "payScale" : "M" } } 如何更改我的查询,以便我能够使用此json {“_ id”搜索它:ObjectId(“5804926140fbe41a7c5afbf0”),“公司”:{“companyName”:“ABC”,“url”:“abc。 com“,”noticePeriod“:50,”payScale“:”M“}}

String URL_KEY = "companies.url";
String NOTICE_PERIOD_KEY = "companies.noticePeriod";
String PAY_SCALE_KEY = "companies.payScale";
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put(URL_KEY, companyUrl);
NoticePay noticePay;
Document mongoDoc = collection.find(whereQuery).projection(fields(include(URL_KEY, NOTICE_PERIOD_KEY, PAY_SCALE_KEY), excludeId())).first();

how can I insert a document in the 2nd form in mongodb using java ? 如何使用java在mongodb中以第二种形式插入文档?

String URL_KEY = "companies.url";
String NOTICE_PERIOD_KEY = "companies.noticePeriod";
String PAY_SCALE_KEY = "companies.payScale";
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("companies.companyName", companyName);
dbObject.put(URL_KEY, companyUrl);
dbObject.put(NOTICE_PERIOD_KEY, noticePeriod);
dbObject.put(PAY_SCALE_KEY, payScale);
collection.insert(dbObject);

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

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