Unable to pull all the documents from MongoDB collection using Jmeter JSR233 sampler.
I’m trying to pull all the documents from MongoDB collection to jmeter to pass it as a test case for my performance test execution.
The below works -
myDoc = collection.find(eq("ABCValue", "ABC")).first();
log.info myDoc.toJson();
collection.find(...query...).last(); also works.
I’m able to pull the first and last value from MongoDB collection for that query. However unable to pull all the documents from the collection when I try to use the following -
myDoc = collection.find();
log.info myDoc.toJson();
This does not work only in Jmeter. Please help!
To print all documents returned from find(), you need to iterate over the documents returned
for (Document cur : collection.find()) {
log.info cur.toJson();
}
The find() method returns a FindIterable() instance that provides a fluent interface for chaining other methods.
Related
I am using official mongo driver for Golang: go.mongodb.org/mongo-driver/mongo
Preface
In this driver, I could not found any method for returning one single object from aggregation query.
driver aggregation documentation
Problem
The problem I am facing with this is if I have some documents which should be filtered and only first one should be returned, then I forcefully need to get all documents and return document on 0 index. In my knowledge, this is not optimized.
I have only one method for aggregation here, which returns cursor of multiple objects:
Is it possible to get single object in aggregation in this driver ?
Aggregation always returns a list of documents, but you may use the $limit stage to only return one document.
bson.M{"$limit": 1}
I am implementing a process in rust where I read a large number of documents from a mongodb collection, perform some calculations on the values of each document and then have to update the documents in mongodb.
In my initial implementation, after the calculations are performed, I go through each of the documents and call db.collection.replace_one.
let document = bson::to_document(&item).unwrap();
let filter = doc! { "_id": item.id.as_ref().unwrap() };
let result = my_collection.replace_one(filter, rec_document, None).await?
Since this is quite time consuming for large record sets, I want to implement it using db.collection.bulkWrite. In version 1.1.1 of the official rust mongodb driver, bulkWrite does not seem to be supported, so I want to use db.run_command. However, I am not sure how to call db.collection.bulkWrite(...) using run_command as I cannot figure out how to pass the command name as well as the set of documents to replace the values in mongodb.
What I have attempted is to create a String representing the command document with all the document records to be updated string joined as well. In order to create bson::Document from that string, I convert the string to bytes and then attempt to create the document to be passed using Document::from_reader but that doesn't work, nor is a good solution.
Is there a proper or better way to call bulkWrite using version 1.1.1 of the mongodb crate?
This is such a weird problem. I think it has to do with how I am querying the document. It seems like the Meteor API has changed to query documents but the docs on the website are the same.
Here is a document in the database:
meteor:PRIMARY> db.studies.findOne()
{ "_id" : ObjectId("56c12e6537014a66b16771e7"), "name" : "Study 1" }
I have subscribed to get all documents and here is what I am trying in the console to get the documents.
var study = Studies.findOne() // This works.
It returns:
_id: MongoID.ObjectID
_str: "56c12e6537014a66b16771e7"
name: 'Study 1'
I just started a new Meteor project with React. I see that my collection is returning _id: MongoId.ObjectId
This is different, I have been using Meteor for awhile with Blaze and I can't remember it returning MongoID.ObjectID instead of just the string
But now if I try and find just that one document, it does not work.
var study = Studies.findOne("56c12e6537014a66b16771e7");
or
var study = Studies.findOne({_id: "56c12e6537014a66b16771e7"});
I am positive I am queuing for the right _id field. I have double checked the ID. Why does trying to find this one document not work?
Please let me know how I can query for a document. Has something changed with Meteor? The documentation still says you can search by id string.
You need to explicitly cast object id string to an ObjectID
var study = Studies.findOne({_id: new Meteor.Collection.ObjectID("56c12e6537014a66b16771e7")});
#Jaco has the correct answer, but I wanted to answer here to clarify what the higher level issue was.
The reason why my find query was not following syntax in Meteor docs is because I inserted the document into MongoDB directly, instead of through the Meteor API.
If you insert the document directly into MongoDB, you have to query the document using the syntax #Jaco mentioned in his answer.
Similar question: Meteor - Find a document from collection via Mongo ObjectId
So instead of changing my query code, I just deleted the document I inserted directly into MongoDB, and inserted a documented using the console in the browser.
Now I can query the document like normal.
So the root of the issue is that if you insert the document directly into MongoDB, you don't get the same type of document as you would if you insert the document using the Meteor API.
In my MongoDB 3.2 based application I want to perform the documents processing. In order to avoid the repeated processing on the same document I want to update its flag and update this document in the database.
The possible approach is:
Query the data: FindIterable<Document> documents = db.collection.find(query);.
Perform some business logic on these documents.
Iterate over the documents, update each document and store it in a new collection.
Push the new collection to the database with db.collection.updateMany();.
Theoretically, this approach should work but I'm not sure that it is the optimal scenario.
Is there any way in MongoDB Java API to perform the followings two operations:
to query documents (to get them from the DB and to pass to the separate method);
to update them and then store the updated version in DB;
in a more elegant way comparing to the proposed above approach?
You can update document inplace using update:
db.collection.update(
{query},
{update},
{multi:true}
);
It will iterate over all documents in the collection which match the query and updated fields specified in the update.
EDIT:
To apply some business logic to individual documents you can iterate over matching documents as following:
db.collection.find({query}).forEach(
function (doc) {
// your logic business
if (doc.question == "Great Question of Life") {
doc.answer = 42;
}
db.collection.save(doc);
}
)
The mongodb $or operator works as intended outside of a meteorjs context:
db.users.find({$or: [{email: 'some#mail.com'},{city: 'atlanta'}]});
I get results for any document that has email some#mail.com or city of atlanta.
The same query in Meteor syntax doesn't yield the same results :
Users = new Meteor.Collection("users");
Users.find({$or: [{email: 'some#mail.com'},{city: 'atlanta'}]});
I've read the meteor docs - http://docs.meteor.com/#find - and since it doesn't say anything about it, I'm assuming it should run just the same as a mongodb 1.6+ instance?
find returns a cursor object. You need to use a fetch to get the array of values. Try:
console.log(Users.find({$or: [{email: 'some#mail.com'},{city: 'atlanta'}]}).fetch());