When using MongoTemplate - collection.findAndModify It will delete all document fields, and leave only the updated column/s.
Why is that?
How to partially update fields in a document?
DBCollection collection = mongoTemplate.getCollection("company");
DBObject query= new BasicDBObject("companyId","1");
DBObject update= new BasicDBObject("phoneNumber","404-525-3928");
DBObject result = collection.findAndModify(query, update);
At this point - all fields removed from company 1...
The workaround will be to go to the DB, fetch company 1 document update the field/s and save it...,
But what if i need to update 10K of them?
You need the $set operator in the update document. You use that and other update operators here otherwise what you specify will replace whatever the document currently contains:
DBCollection collection = mongoTemplate.getCollection("company");
DBObject query= new BasicDBObject("companyId","1");
DBObject update= new BasicDBObject(
"$set", new BasicDBObject("phoneNumber","404-525-3928")
);
DBObject result = collection.findAndModify(query, update);
Related
I would like to create a query such as :
SELECT * FROM MY_TABLE WHERE {MY_DATE_1} BETWEEN {DB_COLUMN_1} AND
{DB_COLUMN_2} OR {MY_DATE_2} BETWEEN {DB_COLUMN_1} AND {DB_COLUMN_2})
I know how to do the opposite ( a field between two variables), since BasicDBObject takes a string (I guess the field name) as first parameter and not a date. Thank you !
I guss you want to use $or to query like the sql
here is the demo with java
BasicDBObject query = new BasicDBObject();
BasicDBObject condition1 = new BasicDBObject("fieldA",new BasicDBObject("$gt","2018-06-02 12:20:00").append("$lt","2019-06-02 12:20:00"));
BasicDBObject condition2 = new BasicDBObject("fieldB",new BasicDBObject("$gt","2018-06-02 12:20:00").append("$lt","2019-06-02 12:20:00"));
BasicDBList condList = new BasicDBList();
condList.add(condition1);
condList.add(condition2);
query.put("$or" ,condList);
collection.find(query);
the $gt means greater than and $lt means less than .
I have the following code and my aim is to retrieve the documents which have the given tags.
String myText = "It is the first #example and is very #important";
ArrayList<String> tags = textProcessor.getTags(myText);
try {
MongoClient mongo = new MongoClient("localhost", 27017);
DB db = mongo.getDB("myFirstDatabase");
DBCollection table = db.getCollection("firstCollection");
BasicDBObject document = new BasicDBObject();
document.put("Text", myText);
document.append("tags", tags);
table.insert(document);
/**** Find and display ****/
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("tags", "#important");
DBCursor cursor = table.find(searchQuery);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
It does not work, because the tagsis an array and I am searching for an element in the array. How can I retrieve the documents which have the given tag (in this case "important") directly?
I know that I can extract all the tags of all documents and then compare them in a loop. But I would like to do that directly.
In the meantime, I am completely new to mongodb. If there is a better way to insert tags in the database (so that, its retrieving is easier), I would be grateful to know.
Thanks in advance,
Mongodb list can be queried through a single string query just fine, you should check the textProcessor.getTags and check the data in the mongodb. I also suggest you to use spring-data-mongodb since it is much simpler and easier to learn.
By default, mongodb adds documents to the end of collection. However, I want them to be added at the first position in the collection. The code I have is,
MongoClient mongo = new MongoClient("localhost", 27017);
DB db = mongo.getDB("test");
DBCollection collection = db.getCollection("lwjsons");
BasicDBObject document = new BasicDBObject();
collection.insert(dbObject);
Use sort when querying.
Just use db.collection.find().sort({_id:-1}).
You are saving documents to collection. It is not like push or unshift in javascript.
I am new to mongodb and I am using a database with a single document in a collection called "Counts". Entering that data is done by the code itself and after entering that data, the document looks like in the first picture.
After that I use the below code sample to read data.
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
DB db = mongoClient.getDB( "OneMedia" );
System.out.println("Connect to database successfully");
DBCollection coll = db.getCollection("Counts");
System.out.println("zzzzzzzzzzzzzzzzzz");
DBCursor curs = coll.find();
Iterator<DBObject> fields = curs.iterator();
while(fields.hasNext()){
BasicDBList List = (BasicDBList) fields.next().get("counts");
BasicDBObject object = (BasicDBObject) List.get(0);
Object value = object.get("comments_count"); /
System.out.println("comments - " + value.toString());
}
Every time I run that code to read data, the document in the collection duplicates and creates another document inside same collection.
Can some one please help me with this.
When you running the code you added a new document. as you say. Since it added to the database it is there even you closed the programe. But if you need to delete all the data in the collection you can use coll.drop(); before you add new document into the database.
I am trying update document with object id but not getting result. Here is my code please help me
DBCollection patients= db.getCollection("Patients");
BasicDBObject doc = new BasicDBObject();
doc.put("name","seshu");
DBObject update=`new` BasicDBObject().append("_id",ObjectId("534e1c8e40a8af540cd01ff4"));
patients`enter code here`.update(update, doc);
When you say "not getting result", I assume you mean that the document is not being updated?
Are you sure that you have the collection name, database name, and ObjectId correct? And that a document exists in that collection with that ObjectId. You should double check all of this via your program or the mongo shell.
Why don't you also try adding some extra checks/debugging in your code, something like this:
DBCollection patients = db.getCollection("Patients");
DBObject update = new BasicDBObject().append("_id", new ObjectId("..."));
long collectionCount = patients.count();
System.out.println(String.format("Collection count: %s", collectionCount));
long count = patients.count(update);
System.out.println(String.format("Count for query: %s", count));
BasicDBObject doc = new BasicDBObject();
doc.put("name", "seshu");
WriteResult writeResult = patients.update(update, doc);
System.out.println(String.format("Updated %s records", writeResult.getN()));
DBObject updated = patients.findOne(update);
System.out.println(updated);