Below is the Employee class
public class Employee {
public List<Department> departments;
public List<Association> association;
....
}
The data is successfully inserted after population for the above class.
MongoCollection.insert(entity);
On retrieving Employee object via Jongo. It returns only the Employee and not departments.
MongoCollection.findOne("{name: '"+name+"'}").as(Employee.class);
sample json file.
{
"_id": ObjectId("577a32894309be1f540cde3e"),
"active": false,
"departments": [
{
"_id": ObjectId("577a32894309be1f540cde3f"),
"description": "sales",
"name": "sales"
}
],
"email": "dinesh.rajul#gmail.com",
"firstName": "dinesh",
"lastName": "regjuk",
"name": "admin",
"password": "hsCIFfVifdOLG3vOUF7jF3x7bEiRCItm7FPiYAVdMW"
}
Could you please help me how to query or get the sub array values ("departments") of the json as well? Currently the list returns null when we say employee.getDeparements()
Thanks
Related
I'm trying to get single field using query. I want to exclude embedded field("necessary") in collection. I'm using springdata and mongodb now.
this is query to get spec without necessary field.
#GetMapping("/selectSpec/{spec_id}") // Select Spec without necessary In Inventory by Spec_id
public Inventory selectSpec(#PathVariable String spec_id){
Query query = new Query();
query.addCriteria(Criteria.where("spec").elemMatch(Criteria.where("_id").is(spec_id)));
query.fields().exclude("spec.necessary").exclude("category").exclude("name").exclude("inventory_id");
return mongoOperations.findOne(query, Inventory.class);
}
I expected query return only one which spec_is is same as I request on postman but it doesn't
{
"spec": [
{
"spec_id": "5",
"parent_id": "2",
"material": "plastic",
"size": "L"
},
{
"spec_id": "6",
"parent_id": "2",
"material": "wood",
"size": "XL"
}
]
}
I expect when I request to get spec_id is 5
{
"spec": [
{
"spec_id": "5",
"parent_id": "2",
"material": "plastic",
"size": "L"
}
this is my inventory collection.
#Document(collection = "inventory")
public class Inventory {
#Id
#Field("inventory_id")
private String inventory_id;
#Field("name")
private String name;
#Field("category")
private List<String> category;
#Field("spec")
private List<Spec> spec;
I tried many ways but I'm still strugging with this problem about weak. please guide, Thank you in advance.
Hello I want to make a query to a sub-collection in firestore I have the following structure
"groups": {
"g1":
{
"name": "Group 1",
"users": {
"u1": {
"id": "user1"
},
"u2": {
"id": "user2"
}
}
},
"g2":
{
"name": "Group 2",
"users": {
"u1": {
"id": "user1"
}
}
}
}
"users": {
"user1": {
"firstName": "Lorem",
"lastName": "Lorem"
},
"user2": {
"firstName": "Lorem2",
"lastName": "Lorem2"
}
}
and I want to make a query that looking for user1 brings me the groups that belong that user in the example would bring me g1 and g2 but if I look for user2 should I only bring g1 can you create a composite index between the group and the user? I am developing it in ionic 4 I don't know if the data is well structured
Thank you very much in what you can help me
You can use an array_contains operation on field users. All that requires is that you know the complete, exact element that the array should contains. If you know all of that, you can check with::
groupsRef.where("users", "array-contains", {
u1": {
"id": "user1"
}
})
If you only know "user1", you will need to have an array that contains only "user1". So for example:
"userids": ["user1", "user2"]
Then you can query with:
itiesRef.where("regions", "array-contains", "user1")
I am creating a service using cosmos db.I am trying to creating a search query.
Query :
SELECT product.Name,product1.Name
FROM catalog
join industry in catalog.Industy
join category in industry.Category
join product1 in category.Product
join Subcategory in category.Subcategory
join product in Subcategory.Product
WHERE CONTAINS(product1.Name,'dg')
But i can not able to get both product and product list . it give me the error. Name already used.
error:
Object creation error, property name 'Name' specified more than once
Tree that i am trying to fetch :
[
{
"id": "string",
"industy": [
{
"id": "string",
"category": [
{
"id": "string",
"subcategory": [
{
"id": "string",
"product": [
{
"id": "string",
"methodOfPreparation": [
{
"id": "string",
}
],
"addons": [
{
"id": "string"
}
]
}
]
}
],
"product": [
{
"id": "string",
"methodOfPreparation": [
{
"id": "string"
}
],
"addons": [
{
"id": "string"
}
]
}
]
}
]
}
]
}
]
expect Output
product[],prodcut1[]
How can i solve this?
Firstly, the error could be solved if you use alias as #Zohar mentioned in the comment.
SELECT product.Name as productName,product1.Name as product1Name
FROM catalog
join industry in catalog.industy
join category in industry.category
join product1 in category.product
join Subcategory in category.subcategory
join product in Subcategory.product
The reason is that every retrieved item is an obj, the format of results is an array consists of many objects. The object can't accept duplicate column names.
If you want to get the format like product[],prodcut1[],you need to loop the result and assemble by yourself.(For example,use stored procedure)
the data is json array
[{ "customer": "John Doe", "items": {"product": "Beer","qty": 6}}, { "staff" : "Jack" }]
Store it as json (or jsonb)
create table sameesh
(
data json
);
insert into sameesh (data)
values
('[{ "customer": "John Doe", "items": {"product": "Beer","qty": 6}}, { "staff" : "Jack" }]');
I am testing the save method of a Restful controller, the CustomerController.
The Customer, the domain class, has an Address association, Address has a City, a City has a State, and a State has a Country.
I wrote custom marshallers and a custom binding helper for Address, so a JSON Customer is like this:
{
"id": "g2c3e08iqton4v5bsgti33gkpd35l0er",
"firstName": "Bob1",
"lastName": "Patiño1",
"middleName": "The Payaso1",
"email": "me#bobpatino.org",
"phoneNumber": "300555551",
"address": {
"addressLine1": "addressLine1",
"postalCode": "110111",
"city": {
"name": "Bogotá",
"state": "DC",
"country": "CO"
}
}
}
Everything is working using that format, remember the custom marshallers and binding helper for Address, but now I want to test the controller.
#Unroll
void "create a new customer firstName: #firstName"(){
when: 'the merchantUser requests to create a new Customer'
controller.request.method = 'POST'
controller.request.contentType = JSON_CONTENT_TYPE
controller.request.JSON = [
firstName: firstName, lastName : lastName, middleName:middleName,
gender: gender, email: email, phoneNumber: phoneNumber, address: address
]
controller.save()
then:'The response code should be CREATED (201)'
controller.response.status == 201
and: 'The new customer should be returned'
controller.response.json.firstName == firstName
//TODO How to validate the address is added?
//controller.response.json.address == ????
1 * controller.springSecurityService.getPrincipal() >> merchantUser
where:
firstName|lastName|middleName|gender|email|phoneNumber|address
JSONObject.NULL |JSONObject.NULL |JSONObject.NULL |JSONObject.NULL |JSONObject.NULL |JSONObject.NULL|JSONObject.NULL
'bob' |'patiño'|'de' |'M' |'bob#patino.co'| '20055555'| [ addressLine1 :'addressLine1', postalCode:'110111',city :[name:'Bogotá',state:'DC',country:'CO']]
}
And my controller save methods is
#Transactional
def save() {
if(handleReadOnly()) {
return
}
def customer = createResource()
customer.merchant = MerchantUser.findByUsername(springSecurityService.getPrincipal().username)?.merchant
customer.validate()
if (customer.hasErrors()) {
respond customer.errors, [status: UNPROCESSABLE_ENTITY] // STATUS CODE 422
return
}
customer.save()
request.withFormat {
json {
respond customer, [status: CREATED] // STATUS CODE 201
}
}
}
If I debug the CustomerController, I found the customer resources is created with a valid Address form the send information, in fact if I run the code it works, but when running the test, the controller.response.json does not contains the address object.
I also had setup the Mock annotation for Customer and Address.
The problem can also be related to the use of custom marshallers in Unit Tests. The controller.response.json is as follows
{
"middleName": "de",
"id": 1,
"lastName": "patiño",
"phoneNumber": "20055555",
"merchant": {
"id": 1,
"class": "models.Merchant"
},
"email": "bob#patino.co",
"address": {
"id": null,
"class": "models.Address"
},
"customerToken": "21jtdik0m99pnarth8g25meb423fmoh0",
"lastUpdated": null,
"gender": "M",
"dateCreated": null,
"class": "models.Customer",
"firstName": "bob"
}
I suspect that you're registering your custom JSON marshaller(s) in BootStrap.groovy, which is not executed for certain types of tests (the precise rules are unclear to me and not well documented)
One way to avoid this limitation is to use the ObjectMarshallerRegisterer approach described here: http://mrhaki.blogspot.com/2013/11/grails-goodness-register-custom.html