JMETER - MongoDB delete record sample script for jmeter - mongodb

Can someone help me on groovy sample code for deleting a record from a MongoDB collection ? Thanks in advance.

Prior to removing an object your need to find one.
Full sample code (my expectation is that you're interested in the last line only, but just in case):
import com.mongodb.* // import all the mongodb.* stuff
import org.apache.jmeter.protocol.mongodb.config.MongoDBHolder;
DB db = MongoDBHolder.getDBFromSource("MongoDBSourceName", "DatabaseName");
DBCollection collection = db.getCollection("CollectionName");
BasicDBObject query = new BasicDBObject("name", "value"); // create DBObject holding the query
DBObject result = collection.findOne(query); // find the document
collection.remove(result); //delete the document
References:
DBCollection class JavaDoc (see remove() and findAndRemove() methods in particular)
How to Load Test MongoDB with JMeter

Related

MongoDB Insert Entity vs a BsonDocument using via C# Driver

I am using the .NET MongoDB driver to insert values into my collection in MongoDB .
I noticed when I insert an entity the objects get stored as simple columns like in the second document below where as when I used a BsonDocument to insert documents they get inserted as an object under the _v column in the first document below.
Can someone explain whats the difference between the two?
Also is it possible to insert the BsonDocument like in the second document via the .NET driver? In my case I have to build the document Dynamically since I dont have a concrete entity to insert with.
The result you're seeing is related to dynamic handling. Mongo driver doesn't know which type you use and save this information in _t field. To fix it you just need to create collection based on BsonDocument directly like:
var coll = db.GetCollection<BsonDocument>("coll");
coll.InsertOne(new BsonDocument("name", "value"));
See this doc
So with the .NET driver:
If I insert the bson object directly the document is inserted as an object with column "_v"
If I insert the bson object after deserializing it using the BsonSerializer it can be inserted as simple columns in MongoDB.
var collection = database.GetCollection<dynamic>(collectionName);
var bson = new BsonDocument();
...
// Fill bson object
...
collection.InsertOne(bson); // Image 1
collection.InsertOne(BsonSerializer.Deserialize<dynamic>(bson)); // Image 2

Delete all documents in a collection with Springdata mongo Query

I want to implement a delete method which deletes all the documents in a collection. I am using mongo db with Spring Data.This could be done using db.myCollection.remove({}) in Mongo shell. But I want to write a method in my data access layer to do this.I am not using MongodbTemplate in my Dao class. I want to know how can I do this with Query.
Query query = new Query();
Could anybody please tell me how can I do it.
You can use MongoTemplate directly
mongoTemplate.remove(new Query(), collectionName);
Use MongoRepository's deleteAll(). Utilizes mongoTemplate behind the scene to call the remove method.
From calling method someRepository.deleteAll()
Drop collection may be efficient as other answer has noted. For that you will need to use MongoTemplate directly and call dropCollection with entity class or collection name.
You can drop the collection and create it again:
mongoTemplate.dropCollection("collectionName");
mongoTemplate.createCollection("collectionName");
You'd better drop the entire collection (if possible) than deleting all documents. For performance and allocation sake.
You can try something such as:
MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 27017));
MongoDatabase db = mongoClient.getDatabase("test");
MongoCollection collection = db.getCollection("test_collection");
collection.drop();
If you do want to delete all (I think), instead of:
collection.drop();
use:
Bson filter = new Document();
collection.deleteMany(filter);
Query is part of spring-data-mongodb, if you can't use a MongoTemplate, probably Query is irrelevant as well.
If you have considerable changes in schema, better is to drop the collection.
MongoTemplate.dropCollection(collectionName).
But the requirement is to delete all documents you have use
MongoTemplate.remove(new Query(), collectionName)or
CrudRepository.deleteAll()
If all collections are to be deleted, this could also be done:
mongoTemplate.collectionNames
.forEach { mongoTemplate.dropCollection(it) }

MongoDB java client's WriteConcern doesn't work

I am a newbie to use MongoDB. I just imported the latest MongoDB java client via Maven:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.2.2</version>
</dependency>
And then I wrote a very simple program to test the insert operation.
//I use a replicaset
MongoClient mongoClient = new MongoClient(
Arrays.asList(new ServerAddress("10.12.16.136", 29017),
new ServerAddress("10.12.16.136", 29018),
new ServerAddress("10.12.16.136", 29019)));
//I just want to write and ignore any database errors. (This does not work)
mongoClient.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
//Get database and collection
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("realtime");
//This does not work too.
collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED);
//Make a simple object to insert. I already have a document with the same _id in the database and I don't want to see the exception via the WriteConcern operation.
Document doc = Document("longitude", longitude)
.append("latitude", latitude)
.append("velocity", velocity)
.append("soc", soc)
.append("_id", 12345678);
//WriteConcern.UNACKNOWLEDGED doesn't work. An exception will be raised.
collection.insertOne(doc);
How can I do the right thing?
That's because collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED); generates a new MongoCollection object with a different write concern which you never use:
/**
* Create a new MongoCollection instance with a different write concern.
*
* #param writeConcern the new {#link com.mongodb.WriteConcern} for the collection
* #return a new MongoCollection instance with the different writeConcern
*/
MongoCollection withWriteConcern(WriteConcern writeConcern);
The following code:
MongoCollection<Document> dup = collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED);
...
dup.insertOne(doc);
should work, i.e. no error raised.
As for the MongoClient level write concern which is not propagated to the database:
public MongoDatabase getDatabase(final String databaseName) {
MongoClientOptions clientOptions = getMongoClientOptions();
return new MongoDatabaseImpl(databaseName, clientOptions.getCodecRegistry(), clientOptions.getReadPreference(),
clientOptions.getWriteConcern(), createOperationExecutor());
}
As you can see, the write concern is taken from MongoClientOptions ignoring the parameter value passed to mongoClient.setWriteConcern() method, which may be a bug.
So, to set a global write concern properly, you will have to create an instance of MongoClientOptions:
MongoClientOptions options = MongoClientOptions
.builder()
.writeConcern(WriteConcern.UNACKNOWLEDGED)
.build();
and pass it to the MongoClient constructor.
I had got this exception, when on empty collection was already present in MongoDB, and i was trying to create one more same collection by mistake, so when i deleted the existing collection and then did a post API, then it worked fine for me..
So better first drop your existing collection and then try to call any CRUD API..
if there is one Java entity with Collection ( name="STUDENT" ) , and you are again trying to create one collection with name student, this error comes

How to create a collection in MongoDB using SPRING data

Am using Spring-Data-Mongo to access do CRUD operations on my mongo database. I execute the below line
DB db = mongoTemplate.getDb()
When am in debug mode I can see that db._collections properties has 4 values (collections that I inserted). But when I query for
db.getCollectionNames()
I get zero collections back. Why is that? Same is also true when I do
db.getCollection("collectionName")
But I know the collections do exists because when I do something like
mongoTemplate.createCollection("collectionName");
I get an exception saying that collection already exists. Can anyone please explain what I might be missing
MongoTemplate provides a few methods for managing collections. The following example demonstrates some of the methods:
DBCollection collection = null;
if (!mongoTemplate.getCollectionNames().contains("collectionName")) {
collection = mongoTemplate.createCollection("collectionName");
}
mongoTemplate.dropCollection("collectionName");
In the above, getCollectionNames() returns a set of collection names and dropCollection() drops the collection.
Use MongoClient, MongoDatabase, and MongoIterable of com.mongodb.client and com.mongodb package.
MongoClient client = MongoClient(<host>, port);
MongoDatabase db = client.getDatabase(<Name of the database>);
MongoIterable<String> = db.listCollectionNames();
And now you can iterate over all the names of the collections.
Additionally, you even can use MongoCollection class to get the Document from the specified collection. The getCollection() will create collection if not present.
MongoCollection<Document> collection = db.getCollection(<Collection name>);

why mongo is fetching results even after deleting the collection?

using pymongo i have deleted a particular collection. when i query the database using mongoengine x.objects.get(y="something") is still showing up with the deleted result.
anyone please help me with this.. Thanks in advance.
You could try using the drop method:
from pymongo import Connection
connection = Connection('localhost', 27017) #Connect to mongodb
print(connection.database_names()) #Return a list of db, equal to: > show dbs
db = connection['testdb1'] #equal to: > use testdb1
print(db.collection_names()) #Return a list of collections in 'testdb1'
print("posts" in db.collection_names()) #Check if collection "posts"
# exists in db (testdb1)
collection = db['coll']
print(collection.count() == 0) #Check if collection named 'coll' is empty
collection.drop() #Delete (drop) collection named 'coll' from db