Nested Select Query With Mongo Repository - java

We use SpringBoot with mongo and have a document like this:
[{
"id": "classicId",
"name": "classicName",
"models": [
{
"id": "AnotherId",
"name": "AnotherSomeName"
},
{
"id": "RequiredId",
"name": "SomeName"
}
]
}]
The id in the array models is unique.
The input could be just any id in the models array. So the user will just give us the value "AnotherId" in order to find the document.
How can we do that in java using the mongo template or mongo repository?

With MongoRepository you could do something like:
public Optional<YourObject> getByModelId(theVariableWithTheValue) {
Query query = new Query().addCriteria(Criteria.where("models.id").is(theVariableWithTheValue));
List<YourObject> result = mongoTemplate.find(query, YourObject.class);
return result.isEmpty() ? Optional.empty() : Optional.of(result.get(0));
}

Related

How to query in top level array in couchbase?

I have a couchbase collection where I am storing each record as an array as below
[
{
"id": 1237,
"customerId": 1561
},
{
"id": 1238,
"customerId": 1562
}
]
Now I want to query this array where customerId = xyz;
How do I achieve this using N1 query?
The document it self is scalar ARRAY vs OBJECT it is hard to create index and might need primary index.
INSERT INTO default VALUES("f01", [ { "id": 1237, "customerId": 1561 }, { "id": 1238, "customerId": 1562
} ]);
SELECT u.*
FROM default AS d
UNNEST d AS u
WHERE u.customerId = 1561;

How to project array element field in Spring Data Mongo DB Aggregation

How to project embedded array element field in Spring Data MongoDB Aggregation with the document sample below, I tried:
project("customers.id")
project("customers.[].id")
project("customers.?.id")
project("$customers.id")
but didn't work.
Result document without projection:
{
"id": "group1",
"name": "Default Identity Management",
"warningThreshold": 900000,
"tariffId": "TR_0001",
"active": false,
"customers": [
{
"id": "1",
"name": "David",
"properties": [
{
"name": "phone",
"value": "678"
}
],
"roles": [
"dev"
]
},
{
"id": "2",
"name": "Peter",
"properties": [
{
"name": "phone",
"value": "770"
}
],
"roles": [
"techsales",
"dev"
]
}
]
}
Expected document like this:
{
"id" : "group1",
"name" : "Group1",
"tariffId" : "TR_0001",
"warningThreshold" : 900000,
"customers" : [
{
"id" : "1",
"name" : "David",
"properties" : [
{
"name" : "phone",
"value" : "678"
}
]
},
{
"id" : "2",
"name" : "Peter",
"properties" : [
{
"name" : "phone",
"value" : "770"
}
]
}
]
}
I would like to include customers[].id, customers[].name, customers[].properties.
I'd been trying to figure this out for a while now, but couldn't. And the other posts here on stackoverflow, and other places on the internet, didn't provide the solution I was looking for.
My problem was similar to the original author's: There's a document, which has a field which is an array of documents. I wanted to query all the top level fields in the document, and exclude a single field from the documents within the array.
s7vr's answer in the comments for the question did the job for me! Just re-posting that here since most people don't go through all the comments, and it is a really useful answer, that saved me from writing a lot of crappy code! :D
AggregationOperation project = new AggregationOperation() {
#Override
public Document toDocument(AggregationOperationContext aggregationOperationContext) {
return new Document("$project", new Document("arrayField.nestedFieldToExclude", 0));
}
};
With Lambda:
AggregationOperation project = aggregationOperationContext -> new Document("$project", new Document("arrayField.nestedFieldToExclude", 0));
Overall pipeline:
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(criteria),
Aggregation.sort(Sort.Direction.DESC, "createdOn"),
project);
I just wish there was a cleaner way to do this with the Spring MongoDB Data API directly, rather than using it this way with lambda functions.
Also, please note that the method AggregationOperation.toDocument(AggregationOperationContext aggregationOperationContext) has been deprecated as of spring-data-mongodb version 2.2.

MongoDB Java Driver aggregation with regex filter

I am using MongoDB Java Driver 3.6.3.
I want to create regex query with group by aggregation to retrieve distinct values.
Let's say I have json:
[{
"name": "John Snow",
"category": 1
},
{
"name": "Jason Statham",
"category": 2
},
{
"name": "John Lennon",
"category": 2
},
{
"name": "John Snow",
"category": 3
}]
I want to create query where regex is like "John.*" and group it by name so there would be only one "John Snow"
Expected result is:
[{
"name": "John Snow",
"category": 1
},
{
"name": "John Lennon",
"category": 2
}]
The answer provided by felix is correct, in terms of Mongo Shell commands. The equivalent expression of that command using the MongoDB Java driver is:
MongoClient mongoClient = ...;
MongoCollection<Document> collection = mongoClient.getDatabase("...").getCollection("...");
AggregateIterable<Document> documents = collection.aggregate(Arrays.asList(
// Java equivalent of the $match stage
Aggregates.match(Filters.regex("name", "John")),
// Java equivalent of the $group stage
Aggregates.group("$name", Accumulators.first("category", "$category"))
));
for (Document document : documents) {
System.out.println(document.toJson());
}
The above code will print out:
{ "_id" : "John Lennon", "category" : 2 }
{ "_id" : "John Snow", "category" : 1 }
You can achieve this with a $regex in $match stage, followed by a $group stage:
db.collection.aggregate([{
"$match": {
"name": {
"$regex": "john",
"$options": "i"
}
}
}, {
"$group": {
"_id": "$name",
"category": {
"$first": "$category"
}
}
}])
output:
[
{
"_id": "John Lennon",
"category": 2
},
{
"_id": "John Snow",
"category": 1
}
]
you can try it here: mongoplayground.net/p/evw6DP_574r
You can use Spring Data Mongo
like this
Aggregation agg = Aggregation.newAggregation(
ggregation.match(ctr.orOperator(Criteria.where("name").regex("john", "i")),
Aggregation.group("name", "category")
);
AggregationResults<CatalogNoArray> aggResults = mongoTemp.aggregate(agg, "demo",demo.class);

Save JSON request as Item in dynamoDb

I have a request of the following JSON format:
{
"profile": {
"created": 1505202655,
"createdBy": "abc",
"updated": 1505202655,
"updatedBy": "xyz"
},
"likesId": [
"0010127916"
],
"icon": null,
"Attributes": {
"backgroundColor": "#FFFFFF",
"logo": "images/Logos/P0010127916.jpg",
"textColor": "#000000"
},
"profileId": "PACYG0010916",
"restrictions": {
"clients": [
"Android",
"SmartTv"
],
"UserTypes": [
"user1",
"user2"
],
"periodEnd": 1512978849,
"periodStart": 1505202849
},
}
I am trying to save the above JSON request Object in the dynamoDb table using putItem. However I am stuck in some issues which are as follows:
Can I store this whole JSON request as-is(without escaping double quotes) in the form of item in dynamodb table?
In case of likesId and Attributes I am storing them as a List and Map with the help of .withList and .withMap methods respectively, but in case of profile I have taken it as a POJO which has 4 states, how can I save this object with the putItem as I did not find any method for saving objects like this, as we have methods for string, numbers and other datatypes, how can I save my own object?
Any kind of guidance will be highly appreciated as I am new to dynamoDb and learning it by doing POC.
You should be able to save it quite easily with the DocumentClient class:
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property
var params = {
TableName : 'Table',
Item: item
};
var documentClient = new AWS.DynamoDB.DocumentClient();
documentClient.put(params, function(err, data) {
if (err) console.log(err);
else console.log(data);
});
Where item is the object from your original question

CosmoDB querying a value within an array using Mongo driver does not seem possible

We are migrating from mongoDB to CosmoDB using the Mongo Java Client. We have encountered the following difference in query behavior with arrays.
We have documents that look something like the following
[{
"name":"garry",
"pets":["cats","dogs"]
},
{
"name":"sally",
"pets":["cats","fish"]
}]
using mongo a query for
find({"pets":"cats"})
will return garry and sally however using cosmoDB we get zero results.
Is there a way to modify the query to replicate the same mongo behavior?
We also have documents that look something like the following that we query on type
[{
"name": "garry",
"pets": [{
"type": "cat",
"name": "Mittens"
}, {
"type": "dog",
"name": "Max"
}]
},
{
"name": "sally",
"pets": [{
"type": "cat",
"name": "Paul"
}, {
"type": "fish",
"name": "Bubbles"
}]
}
]
current mongo queries look like
find({"pets.type": "fish"})
in
I tried to use the query below, it works.
Using MongoShell.
find({"pets": {$all: ["cats"]}})
For Mongo Java Driver
FindIterable<Document> queryResult = collection.find(Filters.all("pets", "cats"));

Categories