Return Distinct Values for an Array Field

Posted in UncategorizedTagged

The following example returns the distinct values for the field sizes from all documents in the Collection1 collection: This will be the expected result: Distinct, as for relational database, finds the distinct values for a specified field across a single collection or view and returns the results in an array.

Mongo Replica set with docker-compose

Posted in IT StuffTagged ,

A replica set in MongoDB is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability and are the basis for all production deployments. Replication provides redundancy and increases data availability. With multiple copies of data on different database servers, replication provides a level of fault tolerance against the loss of a single database….

Find on different collections to create a document using mongoose

Posted in IT StuffTagged , ,

I need to query multiple collections to prepare a MongoDB document and then save it using Mongoose and NodeJS. The solution is to use async.parallel I have two source collections, Robot, Target and the destination collection Activity. For first, async must be added: var async = require(’async’);var async = require(‘async’); then: async.parallel({   robotFind: function(cb)….

Date based query using milliseconds (Java long) time on MongoDB

Posted in IT StuffTagged

Let’s suppose I need to search all records that match a date condition. On MongoDB I’ve a bunch of data like this: { "_id" : "9ed3b937-0f43-4613-bd58-cb739a8c5bf6", "userModels" : { "5080" : { "generated_date_timestamp" : NumberLong(1413382499442), "model_id" : 5080, }, } "values" : {} }{ "_id" : "9ed3b937-0f43-4613-bd58-cb739a8c5bf6", "userModels" : { "5080" : { "generated_date_timestamp" :….

Query date based using milliseconds time on MongoDB

Posted in IT StuffTagged

I need to search all records that match a date condition. On MongoDB I’ve a bunch of data like this: { "_id" : "9ed3b937-0f43-4613-bd58-cb739a8c5bf6", "userModels" : { "5080" : { "generated_date_timestamp" : NumberLong(1413382499442), "model_id" : 5080, }, } "values" : {} }{ "_id" : "9ed3b937-0f43-4613-bd58-cb739a8c5bf6", "userModels" : { "5080" : { "generated_date_timestamp" : NumberLong(1413382499442), "model_id"….

Results pagination with MongoDB and Java

Posted in IT StuffTagged ,

To implement the MongoDB results pagination in Java, there are few parameters that need to be collected: 1) the order of results (ascending / descending) 2) the field used to order the results 3) the number of element and the page selected 4) the field and the value used to filter the results As well….

MongoDB query with logical and condition in Java

Posted in IT StuffTagged , ,

Suppose you need to apply some filters to your MongoDB query, for example to extract some _ids that match a regex condition. This is the way to do that: Query query; query.addCriteria(Criteria.where("_id").in(IDs).and(query_field).regex(".*" + query_value + ".*", "i"));Query query; query.addCriteria(Criteria.where("_id").in(IDs).and(query_field).regex(".*" + query_value + ".*", "i")); In this example I used the Query (see here) and Criteria….

Spring-data: Cannot use a complex object as a key value

Posted in IT StuffTagged , ,

I was trying to figured out how to solve this issue. Basically I’m saving a user profile bean that contains multiple occurrences of other beans inside him. Something like date: public class UserProfile {   List<Class1> classes1; List<Class2> classes2;   Integer int1; Map<String, Class3> classes3;   }public class UserProfile { List<Class1> classes1; List<Class2> classes2; Integer….

Route a message to MongoDB

The requirement is very simple. Route an XML message from rabbitMQ to MongoDB. MongoDB BSON as the data storage and network transfer format for “documents”. BSON is a binary-encoded serialization of JSON-like documents. So, the source message is in a XML format, after getting it from rabbitMQ is necessary to translate into a JSON format….