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

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

Related

Spring Data Mongo Query to query with multiple fields and return in one call

Firstly, the document schema am querying on, is as follows:
{x:10, y:"temp", z:20}
There are multiple other documents in the collection with the same schema as above.
Now, I have a list where each element contains, pair of values belonging to keys x and y. This can be pictured as:
[{10,"temp"}, {20,"temp1"}, .....]
keys -> x y x y
Now, am aware, that if I process the array in a loop and take each pair, I can construct a query like:
query.addCriteria(Criteria.where("x").is(10).and("y").is("temp"))
This will return the document if it matches the AND criteria. I can query with all the pairs in the list in such a manner. But this approach will involve a high number of calls to the data base since for each pair in the list, there is a database call.
To avoid this, Is there any way I can query for all the documents that match this AND criteria for each element in the list, in a single call, using spring data MongoDb Api? Framed differently, I want to avoid looping through the array and making multiple calls, if possible.
You could use Criteria.orOperator to return each Document that match at least one Criteria of your list.
Build your list of Criteria looping over your list
List<Criteria> criteriaList = new ArrayList<>();
for (item : yourList) {
criteriaList.add(Criteria.where("x").is(item.x).and("y").is(item.y));
}
Build your query using orOperator:
Query.query(new Criteria.orOperator(criteriaList.toArray(new Criteria[criteriaList.size()])));

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

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.

How can I sort query result of play morphia with multi field?

I want to query item from mongoDB using morphia, but I found the order method has only one parameter, can I query data with multi order field like:
List items = Item.q().order("-updateTime").order("-createTime");
You just separate the fields with commas. The wiki has examples: https://github.com/mongodb/morphia/wiki/Query#sort
You can use
List items = Item.q().order("-updateTime,createTime,otherField").asList();

Mongodb compare two big data collection

I want to compare two very big collection, the main of the operation is two know what element is change or deleted
My collection 1 and 2 have a same structure and have more 3 million records
example :
record 1 {id:'7865456465465',name:'tototo', info:'tototo'}
So i want to know : what element is change, and what element is not present in collection 2.
What is the best solution to do this?
1) Define what equality of 2 documents means. For me it would be: both documents should contain all fields with exact same values given their ids are unique. Note that mongo does not guarantee field order, and if you update a field it might move to the end of the document which is fine.
2) I would use some framework that can connect to mongo and fetch data at the same time converting it to a map-like data structure or even JSON. For instance I would go with Scala + Lift record (db.coll.findAll()) + Lift JSON. Lift JSON library has Diff function that will give you a diff of 2 JSON docs.
3) Finally I would sort both collections by ids, open db cursors, iterate and compare.
if the schema is flat in your case it is, you can use a free tool to compare the data(dataq.io) in two tables.
Disclaimer : I am the founder of this product.

accessing fieldnames as metadata in mongodb

I have a number of different documents in a mongo collection.
The attrs are all numeric values. I don't know apriori what the fieldnames are (I do but they can vary from doc to doc).
I want to write a program that
a) gets all the unique fieldnames in a collection
b) finds the max and min value of each field in the collection
and then reports it in a tabular form with rows "fieldname, maxvalue, minvalue" or in JSON that is equivalent. I am using pymongo but I don't have to, ruby or js or even java driver is fine.
How do I get programmatic access to the list of unique fieldnames in a collection? That's
the major question. I can manage the rest.
Either you main the list of used key inside your application as part of your application logic in some document inside the same collection or a meta-collection yourself or you have to iterate over all documents to figure out the list of keys...there is nothing in MongoDB helping you here since MongoDB is schemaless.