MongoDB find record sample script for jmeter - jmeter

I want to get accurate results by using the MongoDB script as below :
db.user.find({ "name" : "jxd" })
But,unfortunately,it only give me "OK".

It may happen when more than one entry is returned, i.e. your "user" schema contains multiple documents with the name of "jxd".
Try adding toArray() to the end of your query like:
db.user.find({ "name" : "jxd" }).toArray()
and you should see the relevant output.
For more MongoDB Load Testing tips and tricks refer to How to Load Test MongoDB with JMeter guide

Related

How to perform a Join query on Elastic Search via springboot/java?

I have a springboot application that interacts with elastic search (or as it know now OpenSearch). It can perform basic operations such as search, index etc. I used this as my base (although I replaced high level client since it is deprecated) and to perform queries, I am using #Query annotation mostly (as described in section 2.2 here, although I also used QueryBuilders).
Now, I have an interesting use case - I would like to perform 2 queries at the same time. First query would find a file in elastic search that would contain 3 ids. These 3 ids are ids of other files in the same elastic search. The 2nd query would look for these 3 files and finally return them to me. Now, I can easily do it in 2 steps:
Have a query to find a file containing 3 ids and return it
Have a second query (multisearch query can do bulk search as I understand) to search
for 3 files using info from the first query.
However, I need them to happen within the same query - so within the same query I need to search for a file containing the 3 ids and then perform a search for these 3 files.
So currently my files in elastic search look like so:
{
"docId": "docId57",
"relatedDocs": [
{
"relatedId": "docId1",
"type": "apple"
},
{
"relatedId": "docId2",
"type": "orange"
},
{
"relatedId": "docId3",
"type": "banana"
}
]
}
and my goal is to have a query that will accept docId57 as an arg (so a method findFilesViaJoin(docId57) or something) and return a list of 3 files: file for docId1, file docId2 and file for docId3.
I know it is possible either via nested queries, child/parent queries or good old SQL queries (via jpa/hibarnate).
I attempted to use all of these and was unsuccessful for reasons described below.
Child/parent queries
So for child/parent queries, I attempted to use DSL with #Query but couldn't quite get it since I don't have a solid documentation to refer to (the one that actually helps with java not curls). After some time I found this and this articles - I maybe can figure out how to make it work with child/parent but neither explain how to do mapping. If this approach can do what I want, my question is: how to set up & map parent/child in springboot.
Using SQL queries
So for this one, I need to change my set up to use hibarnate. I used this as my base. It works, the only problem I have is that my SQL queries get ignored. Instead, the search is done based of a method's name, not the content of #Query. So it is as if I don't have an annotation used at all. So using the structure mentioned above, the following method in my app:
#Query("select t from MyModel t where t.docId = ?1")
findByRelatedDocsRelatedId(String id)
will return files that has a relatedId that matches the id passed via method ard id (as oppose to reading query from #Query that tells method to search all docs based on docId). Now, I don't mind using method name as a query to search for something. But then why would I use #Query for? (not to mention how do I create a name that does join). It might be possible that my hibernate is set up wrong (never used it before this week). So question here is, does anybody have a nice complete example of hibarnate being used with elastic search that does join query?
Nested queries
For these queries, I assume that I just need to figure out what to put inside the #Query but due to limited documentation about how to compose nested query I didn't manage to make it even remotely to work. Any concreate documentation on how to create DSL nested query would be appreciated.
Any of the ways I described will work for me. I think child/parent seems the best choice (seeing as they kind created for this purpose) but any will do.

Bulk insert documents but if it exists only update provided fields

I have an index which contains data as follows:
{
"some_field": string, -- exists in my database
"some_other_field": string, -- exists in my database
"another_field": string -- does NOT exist in my database
}
I have a script which grabs data from a database and performs a bulk insert. However, only some of the fields above come from the database as shown above.
If a document already exists, I still want to update the fields that come from the database, but without overwriting/deleting the field that does not come from the database.
I am using the bulk API to do this, however, I lose all data relating to another_field when running the script. Looking at bulk docs, I can't find any options to simply update an existing doc.
I am unable to share the script, but hope this might be enough information to shine some light on possible solutions.
TLDR;
Yes it is use index, as the doc explain:
(Optional, string) Indexes the specified document. If the document exists, replaces the document and increments the version. The following line must contain the source data to be indexed.
But make sure to provide the _id of the document in case of an update.
To understand
I created a toy project to replay and understand:
# post a single document
POST /71177773/_doc
{
"some_field": "data",
"some_other_field": "data"
}
GET /71177773/_search
# try to "update" with out providing an id
POST /_bulk
{"index":{"_index":"71177773"}}
{"some_field":"data","some_other_field":"data","another_field":"data"}
# 2 Documents exist now
GET /71177773/_search
# Try the same command but provide using the Id on the first documents
POST /_bulk
{"index":{"_index":"71177773", "_id": "<Id of the document>"}}
{"some_field":"data","some_other_field":"data","another_field":"data"}
# It seems it worked
GET /71177773/_search
If your question was:
Is Elasticsearch smart enough to recognise I want to update an existing document without providing the Id ?
I am afraid it is not possible.

Running queries dynamically

I have my Sql query stored as value in one of key attributes in MongoDb database.I am able to fetch the records from the MongoDb database.I want to run the sql query assosciated after retrieving it from there.
The format of record is as follow:
{
"Id":2,
"Type":"SqlQuery",
"Syntax":"select id from table_name where id = pid and optional_id in ('AC','SU')",
"ValueSpecifiers":{
"id":"request.id"
}
}
I am beginner in springboot.It would be very helpful if someone could guide me some resource or in correct direction .I am confused about is it possible to hit queries by setting few filter parameters here like id dynamically as it varies with request object.I want to run it dynamically without extending crud repository.Its just like hitting db by native queries.
You could use a character like "?" on your recorded query, and then you replace it with your filters. Example:
In the database you save the query like
select id from table_name where id = pid and optional_id in (?)
And then you retrieve the query to a variable and replace the "?" with the parameters you want to filter. Something like
varQuery.replace("?", "'AC', 'SU'")
or
Replace(varQuery, "?", "'AC', 'SU'")

mgo - reduce update and find to one query only

I find myself having to write two db queries and I would like to know whether it's possible to reduce to one.
I am updating a document and then finding the document again in the database. Is it possible to update and receive the full updated document in the response of the update query? This would save a call to the db.
I should explain that the document I'm saving won't be a complete document which is why I'm having to retrieve it from the database to get all the fields.
Yes, it is possible. Please have a look at the documentation of the Query.Apply method, which runs the findAndModify MongoDB command.
Straight from the documentation, this example increments a counter and prints its new value:
change := mgo.Change{
Update: bson.M{"$inc": bson.M{"n": 1}},
ReturnNew: true,
}
info, err = col.Find(M{"_id": id}).Apply(change, &doc)
fmt.Println(doc.N)

MongoDB Spring Data Hashmap query search for numeric key

The following json data is a part of a document:
"ahashMap" : {
"1" : {
"type" : "STOCK"
}
}
To query above document in MongoDB shell, simply execute the script below:
db.COLS.findOne({'aHashMap.1.type':'STOCK'})
Moreover, to query using Spring Data, it also works for the below codes:
Query q = new Query(Criteria.where("aHashMap.1.type").is("STOCK"));
Col c = mongoOperation.findOne(q, Col.class);
However, it only works for older version of Spring Data, such as 1.7.0. Then I tried to compare the source code with old and new version. For the newest version such as 1.7.2, Spring Data removes the .digit from the query so no result would be found. It would treat as the below query for MongoDB:
db.COLS.findOne({'aHashMap.type':'STOCK'})
I feel confused for this kind of interpretation for Spring Data. Anyone would explain the reason behind and how would I process the query without restructure the document. Thank you.
This bug is fixed in Spring Data MongoDB 1.8.0

Resources