Reputation: 2027
I'm building an Android app, and using the MongoDB Atlas service as my database. It's my first time using MongoDB and I'm surprised at how weak the documentation is.
MongoDB requires me to use the stitch client libraries to interface with the Atlas service. I'm using the latest 'org.mongodb:stitch:0.1.0-SNAPSHOT' library Their example/pattern for performing a FIND is like this...
Document query = new Document()
.append(Review.Field.RESTAURANT_ID, restaurant.getId())
.append(Review.Field.OWNER_ID, getUserId());
getDatabase().getCollection(DBCollections.REVIEWS_RATINGS).find(query).continueWith...
I'm trying to perform a "Distinct" command as described here. It looks like the only method I have for getting info from Mongo is the FIND method, and it looks like the only way I have to customize the query is by altering the query document that I build and send along with the method call. If I create a query document like this...
Document query = new Document()
.append("distinct","Restaurants")
.append("key","type");
...it comes back blank. I want it to return an array of unique restaurant types (Strings) from that collection.
I could perhaps use a pipeline, but that documentation is even more lacking that the documentation for the Stitch libraries. I've tried both Find and Aggregate queries using the Debug console in the Stitch console. It looks like a helpful debugging tool, but it requires the queries to be in a JSON format, and again I haven't found any documentation that describes how to format a query into the JSON schema they want.
I'm hoping someone can show me how to accomplish a "Distinct" MongoDB query on MongoDB Atlas using Stitch.
Upvotes: 1
Views: 4100
Reputation: 2027
Well after a good day of investigating, I DID fine something that resembles a solution, at least it works in the pipeline...I'll check later tonight to see if it works in code using the Stitch client library.
Here's the JSON that works in the Stitch Debug Console:
{
"database": "<DATABASE>",
"collection": "<COLLECTION>",
"pipeline": [
{
"$group": {
"_id": "$category"
}
}
]
}
Executing this returns...
{
"result": [
{
"_id": "Category1"
},
{
"_id": "Category2"
}
]
}
It's not perfect as I really want MongoDB to return an array of strings not an array of objects. But as described here, it's just one Map method away, so I'll take it.
*** EDIT
Okay it's 2:30am here but I finally got the code working. I'll post it here for anyone else that trying to do something similiar...
// the next 2 lines are the import part
List<Document> pipeline = new ArrayList<>();
pipeline.add(new Document("$group", new Document("_id","$category")));
Document args = new Document();
args.put("database", Statics.DB_NAME);
args.put("collection", DBCollections.POIS);
args.put("pipeline", pipeline);
mStitchClient.executePipeline(new PipelineStage("aggregate", Statics.SERVICE_NAME, args)).continueWith...(omitted)
These are all BSON objects. The tricky part is having the bson document within a document.
Upvotes: 2