How to use limit and sort query in wso2esb dataservice experssion - mongodb

I try to query documents and get last value from that result by using following query.
dbname.find({"name":"test"}).limit(1).sort({$natural:-1})
But in result I got all the documents which contains name as test.
Kindly let me know where I made mistake

Try please code
dbname.find({}).limit(1).sort({$natural:-1})
You can use below code also
dbname.findOne({name:'test'}).sort({$natural:-1})

Related

Mongoose / typegoose get array based on start and end index

Is there a way to get an array slice at the mongo db level? I am trying to do something similar to the following: Model.find({filter: option}, startindex, endindex). Currently the only option I found is to do the following:
let result = await Model.find({filter: option});
returh result.slice(startIndex, endIndex)
Unfortunately, this does not work since I have to pull the full record each time. If I can do this at the mongo level that would be great. Thank you for your help!
UPDATE:
After further research I found a possible solution:
Model.find({filter: option}).skip(skip).limit(limit);
it seems with this method I am able to do slice the document array in the mongo db. If you have any other ideas please let me know. Thank you!
from what i know, there isnt a way to get an slice of an array from an document, but there is the select
PS: skip skips the first documents found by the query, and limit limits the amount returned by the query

Execute multiple queries at same time, if the all the queries are valid then only I should get the response in MongoDB

I am trying to execute 2 queries at the same time, for example, refer below:
db.getCollection('Test').find({'color':'red'},{'color':'yellow'});
Assume that color red is present in the one collection and yellow is present in another collection, but I am getting the response only from the first query.
Expectation:
1.If both the queries are present in any of the collection I should get both
responses.
2.If anyone of the query is invalid or element is not present in the collection,
I should not get any response.
Thanks in advance
Since you want to match existence of both values, use $all operator:
db.getCollection('Test').find({color:{$all:["red","yellow"]}})
Edit
I managed to get your output, but I think this could be simplified. I thought about different options and ended up in this query:
db.colors.aggregate([{
$facet:{
cond1:[{$match:{color:"red"}}],
cond2:[{$match:{color:"yellow"}}]
}},
{$project:{match1: "$cond1", match2:"$cond2" ,size1:{$size: "$cond1"},
size2:{$size: "$cond2"}}},
{$project:{result:{$cond:[{$and:[{$gte:["$size1",1]},{$gte:
["$size2",1]}]},{$concatArrays:["$match1","$match2"]},[]]}}}
])
I think there is something wrong with your question.
I think you are looking for something like this db.getCollection('Test').find({color: {$in: ['red','yellow']}});
I hope this will help.

Dsiplay MongoDB Output result in JSP

I have the following query in MongoDB which returns a result.
collection.find(eq("name", name)).forEach(printBlock);
How can I collect the results of the above query and pass it to JSP?
I would like if soemone could give an example on how to do this?
Thanks in advance

Spring data mongoDB GeoNear query with excluding fields

I don't know if I am doing something wrong or it is a bug.
I have the following code:
Query criteria = new Query(Criteria.where("locationTime").gte(
"date-time"));
criteria.fields().exclude("friends");
NearQuery query = NearQuery.near(point).maxDistance(maxDistance)
.num(limit).query(criteria);
GeoResults<Profile> result = mongoTemplate
.geoNear(query, Profile.class);
I am executing the query and profiles near by retrieved correctly according to distance and the "locationTime" criteria but it seems to ignore the excluded field and retrieving the profiles with their friends.
When I use simple query the exclude/include fields works perfectly.
I looked every where and could not find any resemble use-case, please let me know if i am doing something wrong.
Thanks.
There's no way to limit the fields with a geoNear command, as far as I know.
I looked into calling executeCommand to try to work around the limitations of Spring Data, but it looks like they don't even have a way to do it from the raw command.

Mybatis-Need to run dynamic query

I am getting query from database.
Using that query I need to run that query and fetch data into collection.
Is there any way??
I have used resultType instead of resulmap and some how I got desired output.