Create a collection with users - my javacode ok? - java

Im using java/mongodb. I would like to create a table (collection) with some users. Well, its working im just not sure about my coding style. Could be this good if i just want to add 3 new persons to the collection?
BasicDBObject doc = new BasicDBObject();
doc.put("name", "klaus");
doc.put("age", 30);
doc.put("city", "new york");
col.insert(doc);
BasicDBObject doc2 = new BasicDBObject();
doc2.put("name", "mirko");
doc2.put("age", 23);
doc2.put("city", "madrid");
col.insert(doc2);
BasicDBObject doc3 = new BasicDBObject();
doc3.put("name", "jon");
doc3.put("age", 34);
doc3.put("city", "unknown");
col.insert(doc3);
Its look so "long". To add only 3 Persons I have to create always a new BasicDBObject and insert it with the same line code (col.insert(bla))? It cant be ^^ And an other question too: At the first time(doc) im adding name, age and city as column. Why do I have to add "name", "age" and "city" again and again... I just want to add "mirko", 23 and "madrid" for doc2.
The last thing what bugs me is that I can add a new Dokument to the Collection with the same(!) values. I could add a new jon with 34 years and the same city. Is this ok? And if yes, I would like to change it. Howto?
Thank you!

Use json for it..
String json = "{'key' : 'value'}"; /* Create json formatted data here */
DBObject dbObject = (DBObject)JSON.parse(json);
collection.insert(dbObject);

No need to execute insert several times. You can make one insert call for collection of objects:
http://api.mongodb.org/java/2.0/com/mongodb/DBCollection.html#insert(java.util.List)
For example:
List<BasicDBObject> docs = new ArrayList<BasicDBObject>();
BasicDBObject doc = new BasicDBObject();
doc.put("name", "klaus");
doc.put("age", 30);
doc.put("city", "new york");
docs.add(doc);
BasicDBObject doc2 = new BasicDBObject();
doc2.put("name", "mirko");
doc2.put("age", 23);
doc2.put("city", "madrid");
docs.add(doc2);
BasicDBObject doc3 = new BasicDBObject();
doc3.put("name", "jon");
doc3.put("age", 34);
doc3.put("city", "unknown");
docs.add(doc3);
col.insert(docs);

You can always try Morphia,a java library, for MongoDB. It lets you save, retrieve and update POJO objects as documents.
For your second query related to collections with same values, in mongoDB you have the option to use unique indexes which makes sure no two records of same values are inserted in the collection

Related

Populating combobox with document fields

I'm building a Java application connected to MongoDB about films, and I want to create a ComboBox so I can populate it with the film names. My problem is the only way I have found is populating it with the full document, instead of just getting the film name.
This is what I have so far:
DBCursor films = collection.find();
while(films.hasNext()){
comboBox.addItem(films.next());
}
This is how I create the document:
DBCollection table = db.getCollection("films");
BasicDBObject document = new BasicDBObject();
document.put("title", titulo.getText());
document.put("arg", argumento.getText());
document.put("date", fecha.getText());
document.put("genres", genres);
table.insert(document);
Is there a way to use find to only get the film titles and then display only the value? Thanks in advance.
EDIT
In the supposed duplicated question, the question is related to finding a specific document based on one of its fields. That's not what I need, I need to populate a combobox with one field, but I need to get all my documents.
If you want to get only specific fields in your result set, you will need to use find(DBObject query, DBObject projection) and specify the fields to get in the projection parameter as next:
// Retrieve only the title of all the documents that can be found in the collection
DBCursor cursor = collection.find(
new BasicDBObject(), new BasicDBObject("title", Boolean.TRUE)
);
while (cursor.hasNext()) {
// Add the value of the title to the combo box
comboBox.addItem((String) cursor.next().get("title"));
}
Rather than do a find() pass in the keys you want returned. In this case I believe all you want back is the title. So this should work:
DBCursor films = collection.find({},{"_id":0,"title":1});
while(films.hasNext()){
comboBox.addItem(films.next());
}
DBObject and DBCollection are old 2.x classes.
You can try something like with 3.x driver.
import static com.mongodb.client.model.Projections.*;
MongoClient mongoClient = new MongoClient();
MongoDatabase db = mongoClient.getDatabase("test");
MongoCollection<Document> col = db.getCollection("films");
List<String> titles = col.find().projection(fields(include("titles"), excludeId())).map(document -> document.getString("titles")).into(new ArrayList<>());

Update document in MongoDB with Java

I'm using MongoDB 3.2 and MongoDB Java Driver 3.2. I want to update the value the document having its ID. In order to do that I tried to use the following two approaches (found in Stackoverflow and MongoDB Blog):
Approach #1:
for(String docID : expiredDocsIDs) {
Bson filter = Filters.eq("_id", docID);
Bson updates = Updates.set("isExpired", true);
dbCollection.findOneAndUpdate(filter, updates);
}
Approach #2:
expiredDocsIDs.stream()
.forEach(docID -> {
BasicDBObject searchQuery = new BasicDBObject("_id", docID);
BasicDBObject updateFields = new BasicDBObject();
updateFields.append("isExpired", true);
updateFields.append("fetchStatus", "FETCHED");
BasicDBObject setQuery = new BasicDBObject();
setQuery.append("$set", updateFields);
dbCollection.updateOne(searchQuery, setQuery);
});
None of these approaches does not work.
It iterates over the list of documents IDs, executes the code but at the end of the code, when I check the documents in DB there is no any change in the documents' field I tried to update.
How can I update the specific document in MongoDB?
As BlakesSeven correctly noted, the problem was with a casting of _id field. The original code sent this parameter as String while the correct way is to send a parameter of ObjectId type.
The correct and worked code form MongoDB 3.2:
this.trackedEpisodesReg.entrySet().stream()
.filter(ep -> ep.getValue().isExpired())
.forEach(ep -> {
BasicDBObject updateFields = new BasicDBObject();
updateFields.append("isExpired", true);
BasicDBObject setQuery = new BasicDBObject();
setQuery.append("$set", updateFields);
BasicDBObject searchQuery = new BasicDBObject("_id", new ObjectId(ep.getValue().getEpisodeID()));
dbCollection.updateOne(searchQuery, setQuery);
});

how to insert/put a json array in the BasicDBObject

I am using mongo db driver 2.11.2. I am bit puzzled how to insert/add an array to the BasicDBObject. All the example which I come across are does not show how to achieve this :(. In the below example how would I insert employees array in the dbo object ?
/*
{
"company" : "stackoverflow",
"established": "when I started coding"
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
}
*/
BasicDBObject basicDBObject = new BasicDBObject();
basicDBObject.put("company", "stackoverflow");
basicDBObject.put("established", "when I started coding");
System.out.println(basicDBObject.toString());
}
Use the Arrays.asList as a contructor for the list. It's just a list. And .append() the object keys rather than "put". Again, it's just as HashMap interface:
BasicDBObject basicDBObject = new BasicDBObject();
basicDBObject.put("company", "stackoverflow");
basicDBObject.append("established", "when I started coding");
basicDBObject.append("employees", Arrays.<DBObject>asList(
new BasicDBObject("firstName", "john")
.append("lastName", "doe"),
new BasicDBObject("firstName", "anna")
.append("lastName", "smith"),
new BasicDBObject("firstName", "peter")
.append("lastName", "jones")
));
System.out.println(basicDBObject.toString());

How to search a document and remove field from it in mongodb using java?

I have a device collection.
{
"_id" : "10-100-5675234",
"_type" : "Device",
"alias" : "new Alias name",
"claimCode" : "FG755DF8N",
"hardwareId" : "SERAIL02",
"isClaimed" : "true",
"model" : "VMB3010",
"userId" : "5514f428c7b93d48007ac6fd"
}
I want to search document by _id and then update it after removing a field userId from the result document. I am trying different ways but none of them is working. Please help me.
You can remove a field using $unset with mongo-java driver in this way:
MongoClient mongo = new MongoClient("localhost", 27017);
DB db = (DB) mongo.getDB("testDB");
DBCollection collection = db.getCollection("collection");
DBObject query = new BasicDBObject("_id", "10-100-5675234");
DBObject update = new BasicDBObject();
update.put("$unset", new BasicDBObject("userId",""));
WriteResult result = collection.update(query, update);
mongo.close();
The easiest way is to use the functionality in the java driver:
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(new ObjectId("10-100-5675234")));
Update update = new Update();
update.unset("userId"); //the fields you want to remove
update.set("putInYourFieldHere", "putInYourValueHere"); //the fields you want to add
mongoTemplate.updateFirst(query, update, Device.class);
The above code assumes that your "_id" is your mongodb normal "_id" which means that the variable you are looking for must be encased in the new ObjectId().
Long time since this post was opened, but might be useful for someone in the future.
device.updateMany(eq("_id", "whatever"), unset("userId"));
An ugly way is to replace the old version with the new version of you document (no userid).
BasicDBObject newDocument = new BasicDBObject();
newDocument.put("_type", "Device");
newDocument.put("alias", "new Alias name");
// ...
BasicDBObject searchQuery = new BasicDBObject().append("_id", "10-100-5675234");
collection.update(searchQuery, newDocument);
The MongoDB documentation provides a clear answer to this question: use the $unset update operator.

Upgrading mongo search from runCommand to find using Java driver

I am using a Java driver to run some mongo text searches.
An example of my previous code is (where values is a String passed in):
DBCollection coll = db.getCollection("testCollection");
//create the basic object
DBObject searchCmd = new BasicDBObject();
//create the search cmd
searchCmd.put("text", "testCollection"); // the name of the collection (string)
// define the search word
searchCmd.put("search", value); // the term to search for (string)
// define the return values
searchCmd.put("project", new BasicDBObject("score", 1).append("name", 1).append("path", 0).append("_id", 0));
// get the results
BasicDBObject commandResult = db.command(searchCmd);
// Just out the results key
BasicDBList results = (BasicDBList) commandResult.get("results");
then I loop over the "results" and I get for each it score by
// Get the number ii
BasicDBObject element = (BasicDBObject) results.get(ii);
// Now get the score
double score = (double) element.get("score");
I want to upgrade to use find since that seems the way 2.6 and later prefers it. So far I have:
DBCollection coll = db.getCollection("testCollection");
BasicDBObject query = new BasicDBObject();
query.append("$text", new BasicDBObject("$search", value));
DBCursor cursor = coll.find(query);
However, I am not sure how to get the score.
I tried doing something like:
query.append("score", new BasicDBObject("$meta", "textScore"));
But this does not work. I would like to be able to get the name and the score so that I can then insert them into a new collection that will also hold the score.
I can get the name easily by:
while (cursor.hasNext())
{
DBObject next = cursor.next();
String name = next.get("name").toString();
}
But how do I get the score?
I found this interesting page: http://api.mongodb.org/java/current/
it appears that find can take a second DBObject which has the fields.
I created a new object:
BasicDBObject fields = new BasicDBObject();
fields.append("score", new BasicDBObject("$meta", "textScore"));
and I am calling find using:
DBCursor cursor = coll.find(query, fields);
and now I can get the score the same way I can get the name.

Categories

Resources