Spring rest docs nested object - spring-restdocs

We need to deliver a response for a call in the following form:
{
"3" : {
"id" : "3",
"parent" : "1",
"title" : "Folder 2",
"folder" : true
},
"4" : {
"id" : "4",
"parent" : "2",
"title" : "Folder 1.1",
"folder" : true
},
"doc:3" : {
"parent" : "3",
"title" : "Document 3",
"folder" : false
},
"doc:2" : {
"parent" : "2",
"title" : "Document 2",
"folder" : false
},
"doc:1" : {
"parent" : "3",
"title" : "Document 1",
"folder" : false
}
}
How should i document it? The Problem is that the object is a structure with EntryId as key and contains no id when its a document. Is there a option where I can use variables to define keys? Or whats recomended to use in responseFields ?

I had the same problem that Andy Wilkinson is pointing in the comments and instead of hardcode it in the ascii.doc, I did it like so:
,relaxedResponseFields(
fieldWithPath("key").description(getMessage(MessageProperties.TRANSLATION_RESOURCE_MAP_KEY)).type(MessageProperties.TYPE_STRING).optional(),
fieldWithPath("value").description(getMessage(MessageProperties.TRANSLATION_RESOURCE_MAP_KEY_VALUE)).type(MessageProperties.TYPE_MAP).optional(),
fieldWithPath("value[].key").description(getMessage(MessageProperties.TRANSLATION_RESOURCE_MAP_KEY_VALUE_KEY)).type(MessageProperties.TYPE_LONG).optional(),
fieldWithPath("value[].value").description(getMessage(MessageProperties.TRANSLATION_RESOURCE_MAP_KEY_VALUE_KEY_VALUE)).type(MessageProperties.TYPE_STRING).optional()
)
check How to document response fields for an object as Map(HashMap) for more info of the problem and if it really like yours.

Related

Salesforce Rest API - Upsert Array records

I want upsert an array Accounts with api standard. I have read that the composite can be used
PATCH /composite/sobjects/Account/MyExtId__c
{
"allOrNone" : false,
"records" : [{
"attributes" : {"type" : "Account"},
"Name" : "Company One",
"MyExtId__c" : "AAA"
}, {
"attributes" : {"type" : "Account"},
"Name" : "Company Two",
"MyExtId__c" : "BBB"
}]
}
Is it not possible to add a simple json like when integrating 1 single record?
For example:
PATCH /sobjects/Account/MyExtId__c
[
{
"Name" : "Company One",
"MyExtId__c" : "AAA"
},
{
"Name" : "Company Two",
"MyExtId__c" : "BBB"
}
]

Need find query for dynamic multiple nested collections in mongodb

I need to find collections based nested on values.But my collection having dynamic values . See below code. In which image name keys are dynamic ( _DSC9691.jpg , _DSC9514.JPG ) and " key1 " is dynamic items. Now I need to find collection based on component, material, Subtype
{
"_id" : ObjectId("5ce2df8498f10b276cb466c4"),
"num" : "1",
"lat" : "39.941436099965",
"lon" : "-86.0691700063581",
"images" : {
"_DSC9691.jpg" : {
"key1" : {
"component" : "Sleeve",
"condition" : "",
"sub_type" : {
"Auto Sleeve" : true
},
"material" : "",
"misc" : ""
}
}
}}
{
"_id" : ObjectId("5ce2df8498f10b276cb466c7"),
"num" : "4",
"lat" : "39.9413828961847",
"lon" : "-86.0715084495015",
"images" : {
"_DSC9554.JPG" : {
},
"_DSC9514.JPG" : {
},
"_DSC9622.JPG" : {
}
}}
#Nagendran you won't be able to perform those operations because the nested document that you want to watch isn't named properly. I suggest you to rename that field using a common name and try to use the code bellow. Also remember to not use blank spaces on field names like "Auto Sleeve".
Object:
{
"_id" : ObjectId("5ce2df8498f10b276cb466c7"),
"num" : "4",
"lat" : "39.9413828961847",
"lon" : "-86.0715084495015",
"images" : [
{
"name" : "some name",
"key":
{
"component" : "Sleeve",
"condition" : "",
"sub_type" : {
"Auto_Sleeve" : true
},
"material" : "",
"misc" : ""
},
},
{
"name" : "some name 2",
"key":
{
"component" : "Sleeve 2",
"condition" : "",
"sub_type" : {
"Auto_Sleeve" : true
},
"material" : "",
"misc" : ""
},
},
]
}
Query:
db.collection.find({
"images.key.sub_type.Auto_Sleeve": true
})
If you want you can use aggregation framework to filter inside the "images" nested document.
To get a litle bit more information you can access:
https://docs.mongodb.com/manual/aggregation/
https://docs.mongodb.com/manual/tutorial/query-documents/
https://www.mongodb.com/blog/post/6-rules-of-thumb-for-mongodb-schema-design-part-1
https://university.mongodb.com/

MongoDB self join(aggregation) of collection with parent references

Hello I have this collection, with parent references, I need to do self join on this collection, so that output will contain the children to maximum depth in one document and also get the output based on "Type" field.
{
"DescId" : "1",
"Desc" : "Testing",
"ParentId" : "null",
"Order" : 1.0,
"Type" : "A",
"Parent" : null
}
{
"DescId" : "1.1",
"Desc" : "Testing Child 1",
"ParentId" : "1",
"Order" : 1.0,
"Type" : "B",
"Parent" : "Testing"
}
{
"DescId" : "1.2",
"Desc" : "Testing Child 2",
"ParentId" : "1",
"Order" : 2.0,
"Type" : "B",
"Parent" : "Testing"
}
{
"DescId" : "1.1.1",
"Desc" : "Testing grand child 1",
"ParentId" : "1.1",
"Order" : 1.0,
"Type" : "C",
"Parent" : "Testing Child 1"
}
At present I have this output based on answer given to my previous question:
{
"ADescId" : "1",
"ADesc" : "Testing"
}
{
"BDescId" : "1.1",
"BDesc" : "Testing child 1"
}
{
"BDescId" : "1.2",
"BDesc" : "Testing Child 2"
}
{
"CDescId" : "1.1.1",
"CDesc" : "Testing grandchild 1"
}
I have tried doing $graphlookup aggregation on this collection, somehow data seems to get messed up. Please let me know if there is a way to achieve this. Thanks.
Desired output:
{
"ADescId" : "1",
"ADesc" : "Testing",
"BDescId" : "1.1",
"BDesc" : "Testing child 1",
"CDescId" : "1.1.1",
"CDesc" : "Testing grandchild 1"
}
{
"ADescId" : "1",
"ADesc" : "Testing",
"BDescId" : "1.2",
"BDesc" : "Testing Child 2"
}

Segregate results from mongodb based on a particular field

My schema for list collection is-
list:
{
username : "some username",
listitem : "some item",
category : "some category",
status : "some status"
}
db.list.find() gets all the results, but I want to retrieve data based on category where the results with same category value appear together.Here is sample output of
db.list.find()
{
"_id" : ObjectId("585669eb4aa058f4e7972db3"),
"username" : "user1",
"listitem" : "item 1",
"status" : "P",
"category" : "A",
}
{
"_id" : ObjectId("5856a7c64aa058f4e7972db4"),
"username" : "user2",
"listitem" : "item 2",
"status" : "p",
"category" : "B",
}
{
"_id" : ObjectId("5858faedde3ffb11a083e770"),
"username" : "user1"
"listitem" : "item 3",
"status" : "p",
"category" : "A",
}
But I want all my 'A' category results together and 'B' category results together.
that is,output should be like this-
{
"_id" : ObjectId("5856a7c64aa058f4e7972db4"),
"username" : "user2",
"listitem" : "item 2",
"status" : "p",
"category" : "B",
}
{
"_id" : ObjectId("585669eb4aa058f4e7972db3"),
"username" : "user1",
"listitem" : "item 1",
"status" : "P",
"category" : "A",
}
{
"_id" : ObjectId("5858faedde3ffb11a083e770"),
"username" : "user1"
"listitem" : "item 3",
"status" : "p",
"category" : "A",
}
Please someone tell me how to do this.Whether this directly possible through mongodb query or I have to perform operations to format output in this form, when I get result in node js server?
It is a simple use case of sort
db.list.find().sort( { category: 1 } )
All the docs will be sorted by the corresponding category values and hence, all docs with the same category value will be listed together.

MongoDB: How to update an entire document in a collection

In my application I need to edit a document... and while editing the document the current [unchanged] version should be still available to other users. For instance, when I start editing a document, I create a new copy of it in a separate collection, and when done I replace the current version with the new version of the temporary collection.
Let's assume the original document stored in collection users looks like this:
{
"_id" : ObjectId("53b986e2fe000000019a5a13"),
"name" : "Joe",
"birthDate" : "2080-12-11",
"publications" : [
{ "title" : "title 1", "description" : "description 1" },
{ "title" : "title 2", "description" : "description 2" }
]
}
Then, let's assume the document above is copied to another collection (e.g. users.wip) and modified like this:
{
"_id" : ObjectId("53b986e2fe000000019a5a13"),
"name" : "Joe",
"birthDate" : "1980-12-11",
"publications" : [
{ "title" : "bye bye", "description" : "Blah blah" },
{ "title" : "title 2", "description" : "description 2" },
{ "title" : "title 3", "description" : "description 3" }
]
}
How do I replace the whoe document? The problem is that when I try to update the original document with
{ "$set" : {
"name" : "Joe",
"birthDate" : "1980-12-11",
"publications" : [
{ "title" : "bye bye", "description" : "Blah blah" },
{ "title" : "title 2", "description" : "description 2" },
{ "title" : "title 3", "description" : "description 3" }
]
}}
I always get the following error message:
10150 Field name duplication not allowed with modifiers
That said, what's the correct way to update an entire document?
To replace the whole document, you don't use $set, but just provide the new doc to the update call:
db.test.update({"_id": ObjectId("53b986e2fe000000019a5a13")}, {
"_id" : ObjectId("53b986e2fe000000019a5a13"),
"name" : "Joe",
"birthDate" : "1980-12-11",
"publications" : [
{ "title" : "bye bye", "description" : "Blah blah" },
{ "title" : "title 2", "description" : "description 2" },
{ "title" : "title 3", "description" : "description 3" }
]
})
However, with the current 3.0 driver, it would be best to use replaceOne instead:
db.test.replaceOne({"_id": ObjectId("53b986e2fe000000019a5a13")}, {
"name" : "Joe",
"birthDate" : "1980-12-11",
"publications" : [
{ "title" : "bye bye", "description" : "Blah blah" },
{ "title" : "title 2", "description" : "description 2" },
{ "title" : "title 3", "description" : "description 3" }
]
})