Mongoose / typegoose get array based on start and end index - mongodb

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

Related

Change field type for many documents in MongoDb

I'm learning MongoDb, I have many documents like this in my collection:
And I want to update my cust_id field from String to ObjectId. I can use Compass to change type but I have very many documents. I've read from Mongo document but still don't understand. Can you show me how to do this on my document for me to understand clearly.
Thank you very much!
There are a few posts here that hint at the solution.
db.students.find().forEach(function(obj) {
obj.cust_id = ObjectId(obj.cust_id);
db.students.save(obj);
});
I tested this in Atlas 4.0.6 and it worked well. I don't know, however, how well this technique will scale to a large dataset.
Just replace students with your own collection name.
I have tested this function turning a string into number and it worked well
db.students.find().forEach(obj =>{
obj.code_number= parseInt(obj.code_number);
db.students.save(obj);
});
You can also create a new collection with the results to keep the original data and check the results
db.students.find().forEach(obj=> {
obj.code_number= parseInt(obj.code_number);
db.students_new.save(obj);
});

How to use limit and sort query in wso2esb dataservice experssion

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})

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.

how to get the max value of a field in MongoDB

like:
{id:4563214321,updateTime:long("124354354")}
there are always new collections enter the db, so I would like to always get latest updated documents aka the largest update time. how to design the shell script? thanks in advance.
You can use a combination of limit and sort to achieve this goal.
db.collectionName.find({}).sort({"updateTime" : -1}).limit(1)
This will sort all of your fields based on update time and then only return the one largest value.
I would recommend adding an index to this field to improve performance.
This is a repeated question, you can find an answer in this link
Using findOne in mongodb to get element with max id
use like this,
db.collection.find().sort({updateTime:-1}).limit(1).pretty()
as findOne you can do it with this syntax:
db.collection.findOne({$query:{},$orderby:{_updateTime:-1}})

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.