B4J Question Aggregate command in mongodb

lucdrb

Active Member
Licensed User
Longtime User
Hi

I would like to use the distinct command from Mongodb in B4J but I can't find How To:

Syntax in mongoDb is
B4X:
db.collection.distinct(field, query, options)

It Finds the distinct values for a specified field across a single collection or view and returns the results in an array.

Help would be welcome
Thanks in advance

Luc
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is very simple to find distinct values with the aggregate method:

Data:
B4X:
Dim collection As MongoCollection = db.GetCollection("test")
collection.Insert(Array(CreateMap("value": 100), CreateMap("value": 200), CreateMap("value": 200)))

Find distinct values of value field:
B4X:
For Each m As Map In collection.Aggregate(Array(CreateMap("$group": CreateMap("_id": "$value"))))
       Log(m.Get("_id"))
   Next
 
Upvote 0

lucdrb

Active Member
Licensed User
Longtime User
Thanks Erel,

This week-end I've found that the distinct command exist as a database command.
As I think that if it's possible to make it from the database side it would be better, I used it with the RunCommand (Command As Map) As Map
Of the MongoDatabase from the JmongoDB library.

But I'll be very gratefull to have the same RunCommand for all the collections methods (https://docs.mongodb.com/manual/reference/method/js-collection/).

Annyway, thanks you for all that you've allready made.

Luc
 
Upvote 0
Top