Getting output of 2 queries in one mongodb call from java morphia - mongodb

I'm fairly new to mongodb but I was wondering if there's a way by which we can get 2 different results from same mongodb collection in one database call uisng mongo java driver with morphia.
I have a collection accounts and I'm fetching data based on a key accountId. I need below two results/outputs from this collection in one query.
count of all the documents where accountID is 'xyz'
ResultList of first N documents where accountID is 'xyz' AND resultSet is sorted by a timestamp field.
to resolve the second scenario I'm using:
..Query....limit(N).order("TimeField").field("TimeField").filter("accountID =", "xyz").asList();
This is working fine as per expectation but to get the total count (scenario 1) of all documents with accountId = 'xyz' needs another mongodb call, which I want to avoid.

MongoDB doesn't support such batching on queries, unfortunately. You'll have to execute two separate calls.

Related

Query for a list contained in another list With Spring Data MongoDb Criterias

I'm trying to search a list contained another list with Mongodb.
{_id:1,list:[1,2,3,4]}
{_id:2,list:[1]}
{_id:3,list:[1,3,4,6]}
I' going to search with list of strings. lets say list L =[1,2,3,4,5]
for example with the given list L = [1,2,3,4,5] I want document with _id 1 and 2 to be returned. 3 must not be returned since 6 isn't in L.
I found two solutions
one
two
Since I want to use Spring Data MongoDb Criterias, I tried to write the above solution but the code seems to be not working and it returns all the documents. Any idea how to write this mongo query with spring data mongo Criterias
You can use:
List<Integer> l = List.of(1,2,3,4,5);
Query query = new Query();
query.addCriteria(Criteria.where("list").not().elemMatch(new Criteria().nin(l)));

query in mongodb atlas to verify the existence of multiple specific documents in a collection

I have a mongodb collection called employeeInformation, in which I have two documents:
{"name1":"tutorial1"}, {"name2":"tutorial2"}
When I do db.employeeInformation.find(), I get both these documents displayed. My question is - is there a query that I can run to confirm that the collection contains only those two specified documents? I tried db.employeeInformation.find({"name1":"tutorial1"}, {"name2":"tutorial2"}) but I only got the id corresponding to the first object with key "name1". I know it's easy to do here with 2 documents just by seeing the results of .find(), but I want to ensure that in a situation where I insert multiple (100's) of documents into the collection, I have a way of verifying that the collection contains all and only those 100 documents (note I will always have the objects themselves as text). Ideally this query should work in mongoatlas console/interface as well.
db.collection.count()
will give you number of inserts once you have inserted the document.
Thanks,
Neha

Translating MySQL NOT IN to MongoDB

I've recently started using MongoDB, and while translating queries, I've stumbled upon a problem when trying to translate MySQL NOT IN query.
What I'm trying to do is find ids that are not present in another collection. I need a way to return how many of these ids there are and then a list of these ids.
In MySQL, these ids are in a table called songs and are named spotify_id. I am checking against a table named artists where the ids are called track_id.
MySQL Query:
SELECT spotify_id FROM songs WHERE spotify_id NOT IN (SELECT artists.track_id FROM artists)
My current code using MongoDB:
track_ids = artists.find({}, {'track_id': 1})
track_ids_list = [d.get('track_id') for d in track_ids]
# Getting amount of ids not present in artists
tracks_num = songs.count_documents({'spotify_id': {'$nin': track_ids_list}})
# Getting tracks not present in artists
tracks = songs.find({'spotify_id': {'$nin': track_ids_list}})
The MongoDB query seems to return a positive number of documents, even when all of the ids should be present in both collections. The MySQL query works perfectly on the same database.
Is there a better way to perform a NOT IN query in MongoDB?
Thank you!

mongodb bulk records exists validation in spring data

Given a List of mongo document UUIDs, I would like to validate if these IDs have corresponding documents in the DB. What is the efficient way to perform bulk validation?
Lets say my program has 100 id strings and would like to validate all these IDs in one DB call. Is it feasible?
I could do it sequentially (i.e using exists(T id) methods in spring data repository) but I would rather want to do in one call.
My program uses spring data for mongodb. But I am open to any native mongo command. I can code the Query in spring data for it. Also, If any of the ID in the list does not have a document in DB, I want to get such ID in response.
I don't know spring-data but you can use the $in operator and do something like find({ _id : { $in : [ your, ids, here ] } }) and then work from there. If you need a simple yes/no for all of them existing, you can just check the length of your result set vs. the length of your input array.

make a join like SQL server in MongoDB

For example, we have two collections
users {userId, firstName, lastName}
votes {userId, voteDate}
I need a report of the name of all users which have more than 20 votes a day.
How can I write query to get data from MongoDB?
The easiest way to do this is to cache the number of votes for each user in the user documents. Then you can get the answer with a single query.
If you don't want to do that, the map-reduce the results into a results collection, and query that collection. You can then run incremental map-reduces that only calculate new votes to keep your results up to date: http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-IncrementalMapreduce
You shouldn't really be trying to do joins with Mongo. If you are you've designed your schema in a relational manner.
In this instance I would store the vote as an embedded document on the user.
In some scenarios using embedded documents isn't feasible, and in that situation I would do two database queries and join the results at the client rather than using MapReduce.
I can't provide a fuller answer now, but you should be able to achieve this using MapReduce. The Map step would return the userIds of the users who have more than 20 votes, the reduce step would return the firstName and lastName, I think...have a look here.