I'm just switching from MySQL to MongoDB and it's a little confusing. We have our database stored in MongoDB and using Java-Selenium in the front end. I am trying to retrieve just one single data from the database. The code below retrieves all the data present in the database:
DBCursor cursor = dbCollection.find();
while(cursor.hasNext())
{
int i=1;
System.out.println(cursor.next());
i++;
}
This is my database lets say:
{
"name" : "Su_123",
"email" : "test#gmail.com",
"_id" : ObjectId("12345656565656")
}
I want to retrieve just the email field (test#gmail.com) from the document where _id = ObjectId("12345656565656") and store this in a String field.
How do I go about coding this? find() retrieves the entire row.
For newer drivers, since 3.7.1
To get specific document that matches a filter:
Document doc = collection.find(eq("email", "test#gmail.com")).first();
It can be used to find the first document where the field email has the value test#gmail.com. And pass an eq filter object to specify the equality condition.
By the same logic using id:
Document document = myCollection.find(eq("_id", new ObjectId("12345656565656"))).first();
To get specific value of the field from selected document:
String value = (String) doc.get("email");
For older drivers like 2.14.2 and 2.13.3
To get a single document with a query:
BasicDBObject query = new BasicDBObject("email", "test#gmail.com");
cursor = coll.find(query);
try {
while(cursor.hasNext()) {
//System.out.println(cursor.next());
}
} finally {
cursor.close();
}
To see more details for newer Mongodb Java driver.
To see more details for older Mongodb Java driver.
To see more details from Mongodb Official Docs.
Related
I have a document like below:
"application" : "test",
"QA1" : {
"url" : "https://google.co.in",
"db" : {
"userName" : "user",
"password" : "pswd"
}
}
I want to retrieve the value "https://google.co.in" by calling the key "url".
While using the below shell command, I am able to retrieve the value as expected.
db.getCollection('Application_Data').findOne({"application":"test"}).QA1.url
But when I convert it to java code, it is throwing me null pointer exception:
cursor = collection.find(Filters.eq("application","test")).iterator();
while (cursor.hasNext()) {
value=cursor.next().get("QA1.url").toString();
break;
}
I also tried with projection like below to get the required values:
cursor = collection.find(Filters.and(Filters.eq("application","test"))).projection(Projections.fields(Projections.include("QA1.url"),Projections.excludeId())).iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next().get("QA1").toString());
}
This is giving me the output as below:
Document{{url=https://google.co.in}}
It is still not giving me the exact value. Appreciate your help on this.
Mongodb Documents are nested. So say, in your example, when you do cursor.next().get("QA1"), you get the Document under "QA1" property.
So the way to achieve what you want is
cursor.next().get("QA1").get("url"). That, with proper error handling (think exceptions).
A better way to do what you want is to use a mapper like spring-data-mongodb that would map the JSON response of mongodb into a java object for you automatically.
You need to use the Document class's getEmbedded method to extract the nested field's value.
while (cursor.hasNext()) {
Document doc = cursor.next();
System.out.println(doc.toJson());
String urlValue = doc.getEmbedded(Arrays.asList("QA1","url"), String.class);
System.out.println(urlValue); // output below
}
The result is as expected - a string: https://google.co.in
I have two documents
/* 1 */
{
"lastUpdated" : ISODate("2019-01-01T01:01:20.201Z"),
"content" : "someText01",
}
/* 2 */
{
"lastUpdated" : ISODate("2020-11-11T16:11:20.202Z"),
"content" : "someText02",
}
Now I would like to update documents - create new field as copy of lastUpdated field;
In robo3T I can do something like this
db.getCollection('someDocuments').update({},
[
{"$set": {"byMongoQuery": "$lastUpdated", }}
],
{"multi": true}
)
And it works.
Now I would like to do same using java code (mongo-java-driver 3.9.0)
And I do something like
mongodb.getCollection("someDocuments").updateMany(Document.parse("{}"), Document.parse("{$set: {'byJavaApi': '$lastUpdated'}}"))
mongodb is instance of com.mongodb.client.MongoDatabase
Final results is like this:
Value is set as plaintext for some reason.
I'm using
Mongodb 4.2.6
mongo-java-driver 3.9.0
Questions:
How to set this field using java code. I would prefer updateMany as there is a lot of those records
Why results are different. I was expecting there is some common engine in mongdb that executes it. And this is exactly the same query.
Your update is using the MongoDB v4.2 feature of updating using a pipeline. You can code this using MongoDB Java Driver v3.11 or higher. Update a field using value from another field:
Bson query = new Document();
List<Bson> updatePipeline = Arrays.asList(Fiters.eq("$set", Filters.eq("byMongoQuery", "$lastUpdated")));
UpdateResult result = collection.updateMany(query, updatePipeline);
I am inserting json file into Mongodb(with Scala/Play framework) and the same getting/downloading it into my view page for some other requirement, but this time it is coming with one "_id" parameter in the json file.
But I need only my actual json file only that is not having any any "_id" parameter. I have read the Mongodb tutorial, that by default storing it with one _id for any collection document.
Please let me know that how can I get or is there any chance to get my actual json file without any _id in MongoDB.
this is the json result which is storing in database(I don't need that "_id" parameter)
{
"testjson": [{
"key01": "value1",
"key02": "value02",
"key03": "value03"
}],
"_id": 1
}
If you have a look at ReactiveMongo dev guide and to its API, you can see it support projection in a similar way as the MongoDB shell.
Then you can understand that you can do
collection.find(selector = BSONDocument(), projection = BSONDocument("_id" -> 0))
Or, as you are using JSON serialization:
collection.find(selector = Json.obj(), projection = Json.obj("_id" -> 0))
You can use this query in the shell:
db.testtable.find({},{"_id" : false})
Here we are telling mongoDB not to return _id from the collection.
You can also use 0 instead of false, like this:
db.testtable.find({},{"_id" : 0})
for scala you need to convert it in as per the driver syntax.
Is it possible to get the number of fields in document using a Query From MongoDB java Driver
Example:
Document1 :{_id:1,"type1": 10 "type2":30,"ABC":123,"DEF":345}
Document2 :{_id:2,"type2":30,"ABC":123,"DEF":345}
Note: In second document "type1" key doesnt exist .
When i project
Is it possible to project only "type1" and "type2" and get number of fields existing in that document.
With current code i am getting all the documents and individually searching if there is the key i am looking is present in the whole cursor:
The code snipped is as follows:
MongoClient mcl=new MongoClient();
MongoDatabase mdb=mcl.getDatabase("test");
MongoCollection mcol=mdb.getCollection("testcol");
FindIterable<Document> findIterable = mcol.find();
MongoCursor<Document> cursor = findIterable.iterator();
//Here am having 120 types checking if each type is present..
while(cursor.hasNext())
{
Document doc=cursor.next();
int numberOfTypes=0;
for(int i=1;i<=120;i++)
{
if(doc.containsKey("type"+i))
{
numberOfTypes++;
}
}
System.out.println("*********************************************");
System.out.println("_id"+doc.get("_id"));
System.out.println("Number of Types in this document are "+numberOfTypes);
System.out.println("*********************************************");
}
}
This code is working if the records are less it wont be over load to the application ..Suppose there are 5000000 with each Document containing 120 types , the application is crashing as there would be more garbage collection involved as for every iteration we are creating a document.Is there any other approach through which we can achieve the above stated functionality.
From your java code I read
project only "type1" and "type2" and get number of fields existing in that document.
as
project only type[1..120] fields and number of such fields in the document
With this assumption, you can map-reduce it as following:
db.testcol.mapReduce(
function(){
value = {count:0};
for (i = 1; i <= 120; i++) {
key = "type" + i
if (this.hasOwnProperty(key)) {
value[key] = this[key];
value.count++
}
}
if (value.count > 0) {
emit(this._id, value);
}
},
function(){
//nothing to reduce
},
{
out:{inline:true}
});
out:{inline:true} works for small datasets, when result fits into 16Mb limit. For larger responses you need to output to a collection, which you can query and iterate as usual.
In mongodb, i can find all those records in a collection in database db that contain a particular field using the following query
var doc = db.collection_name.find({field_name:{$exists:true}})
Now consider the following document:
{
"somefield":"someval",
"metadata": {"id":"someval",
"client_url":"http://www.something.com"
}
}
What would be the query for getting all records having the id field in metadata ?
Please Help.
Thank You
You can use dot notation to reference sub-document fields
var doc = db.collection_name.find({"metadata.id":{$exists:true}})