Spring REST Docs - parts not documented: - rest

I have a springBoot 2.1.9.RELEASE application that uses Spring REST Docs.
I have this payload
{
"hostel" : [ {
"bookingHostelIdentifier" : {
"internalMasterIdentifier" : {
"id" : "987654321"
}
}
}, {
"bookingHostelIdentifier" : {
"customerIdentifier" : {
"id" : "23456789"
}
}
}, {
"bookingHostelIdentifier" : {
"internalMasterIdentifier" : {
"id" : "87654321"
}
}
} ]
}
which I documented like this
fieldWithPath("hostel[]").description("The list of hostels"),
fieldWithPath("hostel[].name").description("The list of hostels"),
fieldWithPath("hostel[].bookingHostelIdentifier").description("The list of hostels"),
fieldWithPath("hostel[].bookingHostelIdentifier.internalMasterIdentifier.id").description("The list of hostels"),
fieldWithPath("hostel[].bookingHostelIdentifier.customerIdentifier.id").description("The list of hostels")
but I got this Exception
org.springframework.restdocs.snippet.SnippetException:
Fields with the following paths were not found in the payload:
[hostel[].bookingHostelIdentifier.internalMasterIdentifier.id, hostel[].bookingHostelIdentifier.customerIdentifier.id]

As the internalMasterIdentifier and the customerIdentifier fields are not always present in a bookingHostelIdentifier, you should mark those fields or the id fields that are nested beneath them as optional.
fieldWithPath("hostel[].bookingHostelIdentifier.internalMasterIdentifier.id").optional().description("The list of hostels"),
fieldWithPath("hostel[].bookingHostelIdentifier.customerIdentifier.id").optional().description("The list of hostels")

Related

MongoDb search for all documents where some filed inside array of array is equals to some value

I need to perform a filter, through the azure portal, on a MongoDb collection, I need to find all documents where the field SystemServerId is equals to 125104
this is a basic version of the document:
{
"_id" : "613a7c1ce007cf00167cd07a",
"UserId" : 334645,
"Events" : [
{
"UserManagerId" : 334645,
"Method" : "MethodXXX",
"EventLogs" : [
{
"Classe" : "ClassXXX"
}
],
"SystemTracer" : {
"UsersServers" : [
{
"SystemServerId" : 125104
}
]
}
}
]
}
I read some documents on MongoDb site, about aggregation, but, I didn't managed how to make this filter, what I tried:
{ Events: { "SystemTracer.UsersServers": { SystemServerId:125104 } } }
{ "Events": { "SystemTracer": { "UsersServers": { SystemServerId:125104 } } } }
And a lot of variations of this filter...
Well, I can filter the documents on collection ?
you can use $elemMatch
db.collection.find({Events:{$elemMatch:{"SystemTracer.UsersServers":{$elemMatch:{SystemServerId:125104}}}}})

How to find special characters in a particular attribute of json data stored in mongodb using mongo query

how to search any special characters in a particular nested json object field. Am having a field which stores nested json data.
I need to write a MongoDB query to fetch all the names which is having special characters
Student collection:
Example:
{
_id:123
student: {
"personalinfo":{
"infoid": "YYY21"
"name": "test##!*"
}
}
}
I have tried few regular expressions but I am not sure how to loop in array elements
I expect it to print the infoid & name, which has special characters in the name field.
The following query can do the trick:
db.collection.distinct("student.personalinfo.name",{"student.personalinfo.name": { $not: /^[\w]+[\w ]*$/ } })
Data set:
{
"_id" : ObjectId("5d77a5babd4e75c58d59821d"),
"student" : {
"personalinfo" : {
"infoid" : "YYY21",
"name" : "test##!*"
}
}
}
{
"_id" : ObjectId("5d77a5babd4e75c58d59821e"),
"student" : {
"personalinfo" : {
"infoid" : "YYY21",
"name" : "Bruce##"
}
}
}
{
"_id" : ObjectId("5d77a5babd4e75c58d59821f"),
"student" : {
"personalinfo" : {
"infoid" : "YYY21",
"name" : "Tony"
}
}
}
{
"_id" : ObjectId("5d77a5babd4e75c58d598220"),
"student" : {
"personalinfo" : {
"infoid" : "YYY21",
"name" : "Natasha"
}
}
}
Output:
[ "test##!*", "Bruce##" ]
Try using below regex, it matches all unicode punctuation and symbols.
dbname.find({'student.name':{$regex:"[\p{P}\p{S}]"}})

How to retrieve Mongodb child value without parents object/field name

Is there a way to query one type of child objects (child object/field with same name under different parents object/field ) directly without invoking the parents name in the find() command
For example I have
MondoDB
{
eatable.fruits.tomato
}
{
eatable.vegetables.tomato
}
Here each tomato is a parameter which have some value assigned in it, And I have tomato under two different objects/fields,
Is there a way to query and retrieve all values of tomato without using the field/object names "fruits" or "vegetables" in the find () command.
As of now, there is no direct way to search for child fields without knowing the parent fields but we do have the workaround. The idea is to first filter the documents by adding the constraints in find() method and then iterate over the response and print out the values of 'tomato' key where ever its present as leaf element.
Following is the example:
db.collection.find().forEach(
function processDoc(document){
let keys = Object.keys(document);
keys.forEach(
function(key){
let value = document[key];
if(typeof value === typeof Object()){
processDoc(value); // Its an embedded document, thus process it again
}else if(key == 'tomato'){
print(value);
}
}
);
}
);
Data set:
{
"_id" : ObjectId("5d63ac599d32d1c15cf5ea5e"),
"eatable" : {
"vegetables" : {
"tomato" : "veg1"
},
"fruits" : {
"tomato" : "fru1",
"banana": "fru1"
},
"misc" : {
"item" : {
"tomato" : "misc1"
}
}
}
}
{
"_id" : ObjectId("5d63ac599d32d1c15cf5ea5f"),
"eatable" : {
"fruits" : {
"tomato" : "fru2"
}
}
}
Output:
veg1
fru1
misc1
fru2

MongoDB: Finding a value where object name contains url

We are using learninglocker and I am trying to query its mongodb. learninglocker puts escaped urls as object names, which makes them more difficult to search. I am returning 0 results when I would expect to return several.
My find is as follows:
{"statement.object.definition.extensions.http://activitystrea&46;ms/schema/1&46;0/device.device_type": "app"}
I assume that this should be escaped somehow, however, am unsure how.
http://activitystrea&46;ms/schema/1&46;0/device
Sample object:
"statement": {
"version" : "1.0.0",
"actor" : { },
"verb" : { },
"context" : { },
"object" : {
"definition" : {
"extensions" : {
"http://activitystrea&46;ms/schema/1&46;0/device" : {
"device_type" : "app"
}
}
}
}
}

Update a Map value document value using Mongo

I have a document like myPortCollection
{
"_id" : ObjectId("55efce10f027b1ca77deffaa"),
"_class" : "myTest",
"deviceIp" : "10.115.75.77",
"ports" : {
"1/1/x1" : {
"portId" : "1/1/x1",
healthState":"Green"
I tried to update
db.myPortCollection.update({
{ deviceIp:"10.115.75.77"},
{ "chassis.ports.1/1/x10.rtId":"1/1/x10" },
{ $set: { "chassis.ports.1/1/x10.healthState" : "Red" }
})
But I am getting error that attribute names mentioned is wrong,.Please help in specifying the syntax properly for embedded map document update.
The "query" portion is wrong as you have split conditions into two documents. It should be this:
db.myPortCollection.update(
{
"deviceIp":"10.115.75.77",
"chassis.ports.1/1/x10.rtId":"1/1/x10"
},
{ "$set": { "chassis.ports.1/1/x10.healthState" : "Red" } }
)
And as long as the query then matches ( valid data not shown in your question ) then the specified field will be set or added.