Paging not working with spring boot and mongodb - mongodb

I saw several articles here on stackoverflow but I couldn't get my pagination working with mongodb and spring boot. The code always returns only one result when I change the value of the limit or skip variable the result is always nothing.
My Repository:
public interface CategoryProductRepository extends MongoRepository<CategoryProduct, String> {
#Aggregation(pipeline = {
"{ '$match': { 'categories.category_id' : ?0 } }",
"{ '$skip' : ?1 }",
"{ '$limit' : ?2 }"
})
List<CategoryProduct> findCategoriesCategoryId(
Long field0,
Long skip,
Long limit
);
}
My structure:
[
{
"product_id": 4,
"id_colletion": 4,
"product_tite": "Testar",
"product_quant": 50,
"product_slug": "slug6",
"product_active": "ativo",
"product_description": "description",
"product_price_un": 50,
"product_price_kg": 10.25,
"star": 0,
"photos": [
{
"photo": "url",
"type": "mini",
"title": "título",
"active": "ativo"
},
{
"photo": "url 2",
"type": "mini",
"title": "título 2",
"active": "ativo"
}
],
"categories": [
{
"category_id": 1,
"name": "categoria 1",
"active": "ativo"
},
{
"category_id": 2,
"name": "categoria 2",
"active": "ativo"
}
]
},
{
"product_id": 5,
"id_colletion": 5,
"product_tite": "new",
"product_quant": 50,
"product_slug": "slugnew",
"product_active": "ativo",
"product_description": "descriptionnew",
"product_price_un": 50,
"product_price_kg": 10.25,
"star": 0,
"photos": [
{
"photo_id": 13,
"photo": "photo 11",
"type": "mini",
"title": "photo 11",
"active": "ativo"
}
],
"categories": [
{
"category_id": 1,
"name": "categoria 1",
"active": "ativo"
}
]
},
{
"product_id": 9,
"id_colletion": 9,
"product_tite": "2",
"product_quant": 2,
"product_slug": "slugnew2",
"product_active": "ativo",
"product_description": "2",
"product_price_un": 2,
"product_price_kg": 2,
"star": 0,
"photos": [
{
"photo": "photo 2",
"type": "mini",
"title": "photo 2",
"active": "desativo"
}
],
"categories": [
{
"category_id": 2,
"name": "categoria 2",
"active": "ativo"
}
]
}
]
Does anyone have any idea where my mistake is?
[SOLVED]
I was inserting the limit and skip wrong but the code works fine

Related

How to fetch data from more than one collection mongodb

I have data from several collections, I'm try to fetch data from mongoDB using Go.
Here is the json I want to display:
{
"status": "ok",
"total": 59,
"subtotal": 10,
"values": [
{
"name": "Anita",
"age": 24,
"other": {
"a": "abc",
"b": "qwe"
}
"detail": [
{
"detail": "Detail a",
"description": "Desc"
},
{
"detail": "Detail b",
"description": "Desc"
}
]
},
{
"name": "Banji",
"age": 23,
"other": {
"a": "abc",
"b": "qwe"
}
"detail": [
{
"detail": "Detail a",
"description": "Desc"
}
]
}
]
}
How to make json data like above using go?

How to check $exists in one to many relationship mongodb sails js?

Is there any way to find records that collection is empty?
For example please find below array. I want only that records with index "companydata" is empty. and also how can i get data that does not have empty "companydata" data.
Thanks in advance.
[
{
"company_id": {
"company_name": "C2",
"slug": "c2",
"is_organized": 1,
"status": "1",
"id": "5adf158f547f7f0314ca8b56",
"companydata": []
},
"user_id": "5ab889aee74a151b50d04ec1",
"status": "0",
"id": "5ae014e7432e85298081be0b"
},
{
"company_id": {
"company_name": "My Compnay",
"slug": "my-compnay",
"is_organized": 1,
"status": "1",
"id": "5ad442d98a0e0c1358ca93df",
"companydata": [
{
"name": "Bhavesh Amin",
"company_id": "5ad442d98a0e0c1358ca93df",
"status": "0",
"id": "5ad442da8a0e0c1358ca93e0"
}
]
},
"user_id": "5ab889aee74a151b50d04ec1",
"status": "0",
"id": "5ae01388432e85298081bdf8"
},
{
"company_id": {
"company_name": "Organization Name",
"slug": "organization-name",
"is_organized": 1,
"status": "1",
"id": "5ad08f9b938d1131eceea624",
"companydata": [
{
"name": "Helen H. Langley",
"company_id": "5ad08f9b938d1131eceea624",
"status": "1",
"id": "5ad08f9b938d1131eceea625"
}
]
},
"user_id": "5ab889aee74a151b50d04ec1",
"status": "0",
"id": "5ad42a5f52851a2b1449db2d"
},
]

Update single sub document in multiple array in mgo

I have following mongodb (3.4.x) document, i code in golang with mgo driver
{
"id": "5981d4c2795a1b4a801ee027",
"scenarioId": "59804b10d8ee910085e33865",
"messages": [
{
"id": "5981d4c2795a1b4a801ee028",
"toQueue": [
{
"id": "5981d4c2795a1b4a801ee029",
"to": {
"email": "some#email.com"
},
"channel": "EMAIL",
"toType": "EMAIL",
"status": {
"id": 1,
"groupId": 1,
"groupName": "PROCESSING",
"name": "APPROVED",
"description": "MessageChain approved for processing"
}
},
{
"id": "5981d4c2795a1b4a801ee02a",
"to": {
"phone": "+381631891245"
},
"channel": "SMS",
"toType": "PHONE",
"status": {
"id": 1,
"groupId": 1,
"groupName": "PROCESSING",
"name": "APPROVED",
"description": "MessageChain approved for processing"
}
}
],
"status": {
"id": 1,
"groupId": 1,
"groupName": "PROCESSING",
"name": "APPROVED",
"description": "MessageChain approved for processing"
}
},
{
"id": "5981d4c2795a1b4a801ee02b",
"toQueue": [
{
"id": "5981d4c2795a1b4a801ee02c",
"to": {
"phone": "+123456789"
},
"channel": "SMS",
"toType": "PHONE",
"status": {
"id": 1,
"groupId": 1,
"groupName": "PROCESSING",
"name": "APPROVED",
"description": "MessageChain approved for processing"
}
}
],
"status": {
"id": 1,
"groupId": 1,
"groupName": "PROCESSING",
"name": "APPROVED",
"description": "MessageChain approved for processing"
}
}
],
"messageStages": [
{
"id": "5981d4c2795a1b4a801ee033",
"validityPeriod": 5,
"validityPeriodTimeUnit": "MINUTES",
"providerId": 5,
"email": {
"text": "this is the email text",
"subject": "and here the email subject"
},
"status": {
"id": 1,
"groupId": 1,
"groupName": "PROCESSING",
"name": "APPROVED",
"description": "MessageChain approved for processing"
}
}
]
}
and I know the value of messages.$.toQueue.id, where i want to update status.id in the related toQueue Array Item.
I tried to do it that way:
query = bson.M{
"messages.toQueue._id": toQueueId,
}
update = bson.M{
"$set": bson.M{
"messages.$.toQueue.$.status.id": status.Id,
"messages.$.toQueue.$.status.name": status.Name,
"messages.$.toQueue.$.status.groupId": status.GroupId,
"messages.$.toQueue.$.status.groupName": status.GroupName,
"messages.$.toQueue.$.status.description": status.Description,
},
}
err = cr.Update(query,update)
but multiple $ are not allowed. Without it's also not updateable.
Is there any way to update only the subdocument, which i found in query ?
Neil Lunn gave the right answer.
It's not possible to work with nested arrays the way I tried to do. And he is also right, that problem is coming from design mistake.
So I changed it as him and Markus W Mahlberg suggested to move the toQueue part in a separate collection, with messageId as a reference.

How would I write a query to give me the following results?

I have 2 classes.
Activities and Comments.
Activities, is linked to comments.
Comments, has the fields username, comment and created_at
When I execute select from Activities, this is an example of my current results. Below, you will see an example of my desired results (Example 2)
Example 1
{
"#type": "d",
"#rid": "#28:38",
"#version": 10,
"#class": "Activities",
"activity_type_id": 1,
"username": "johnt",
"title": "random new activitie test",
"image": "http://www.someurl.com/images/blah-2.png",
"comments": [
"#24:26",
"#24:27",
"#24:28",
"#24:29",
"#24:30",
"#24:31"
],
"Activity_Properties": {
"comment_count": 0,
"share_count": 0,
"like_count": 0
}
}
my desired results are the following
Example 2
{
"#type": "d",
"#rid": "#28:38",
"#version": 10,
"#class": "Activities",
"activity_type_id": 1,
"username": "ultimaterandomuser",
"title": "random new activitie test",
"image": "http://www.someurl.com/images/blah.png",
"comments": [
{
"username": "randomuser1",
"comment": "randomuser1 comment"
"created_at": "2016-06-23 00:00:00",
},
{
"username": "randomuser2",
"comment": "randomuser2 comment"
"created_at": "2016-06-23 00:00:00",
},
{
"username": "randomuser3",
"comment": "randomuser3 comment"
"created_at": "2016-06-23 00:00:00",
}
],
"Activity_Properties": {
"comment_count": 0,
"share_count": 0,
"like_count": 0,
}
}
I tried your case with this simple dataset:
Output Query 1:
SELECT FROM Activities
{
"result": [
{
"#type": "d",
"#rid": "#12:0",
"#version": 1,
"#class": "Activities",
"activity_type_id": 1,
"username": "ultimaterandomuser",
"title": "random new activitie test",
"image": "http://www.someurl.com/images/blah.png",
"comments": [
"#13:0",
"#13:1",
"#13:2"
],
"Activity_Properties": {
"comment_count": 0,
"share_count": 0,
"like_count": 0
},
"#fieldTypes": "comments=n"
}
],
"notification": "Query executed in 0.01 sec. Returned 1 record(s)"
}
Output Query 2:
SELECT expand(#this.exclude('comments')) FROM (SELECT *, $comments as commentsList FROM Activities
LET $comments = (SELECT expand(#this.exclude('#rid')) FROM (SELECT username, comment, created_at FROM Comments WHERE #rid IN $parent.current.comments)))
{
"result": [
{
"#type": "d",
"#rid": "#12:0",
"#version": 1,
"#class": "Activities",
"activity_type_id": 1,
"username": "ultimaterandomuser",
"title": "random new activitie test",
"image": "http://www.someurl.com/images/blah.png",
"Activity_Properties": {
"comment_count": 0,
"share_count": 0,
"like_count": 0
},
"commentsList": [
{
"#type": "d",
"#version": 0,
"username": "randomuser1",
"comment": "randomuser1 comment",
"created_at": "2016-06-25 03:33:13",
"#fieldTypes": "created_at=t"
},
{
"#type": "d",
"#version": 0,
"username": "randomuser2",
"comment": "randomuser2 comment",
"created_at": "2016-06-25 03:33:13",
"#fieldTypes": "created_at=t"
},
{
"#type": "d",
"#version": 0,
"username": "randomuser3",
"comment": "randomuser3 comment",
"created_at": "2016-06-25 03:33:13",
"#fieldTypes": "created_at=t"
}
],
"#fieldTypes": "commentsList=z"
}
],
"notification": "Query executed in 0.041 sec. Returned 1 record(s)"
}
Hope it helps

Multi-level sorting or orderBy in mongoDB

I am working on mongoDB with Laravel using jenssegers . and i am trying to get
documents on sorting based on layout.price.
Is this possible in mongodb with laravel. ?
I am using limit and skip for pagination , so i have to get orderBy or sort direct from Database.
[{
"title": "I am first document",
"design": "mango",
"description": "Loremipsum",
"dimension": [
{
"id": "542549383d1784901000002c"
}
],
"layout": [
{
"id": "542411813d17849810000029",
"price": NumberInt(69000)
}, {
"id": "542411813d17849810000030",
"price": NumberInt(2000)
}
],
"images": [
"uploads/sofa/QRSTU695a6_0.png"
],
"product_type": NumberInt(1),
"updated_at": ISODate("2014-10-08T08:47:26.0Z"),
"created_at": ISODate("2014-09-29T11:13:52.0Z")
},
{
"title": "I am second document",
"design": "mango2",
"description": "Loremipsum",
"dimension": [
{
"id": "542549383d1784901000002d"
}
],
"layout": [
{
"id": "542411813d17849810000031",
"price": NumberInt(80000)
}, {
"id": "542411813d17849810000032",
"price": NumberInt(27000)
}
],
"images": [
"uploads/sofa/QRSTU695a6_0.png"
],
"product_type": NumberInt(1),
"updated_at": ISODate("2014-10-08T08:47:26.0Z"),
"created_at": ISODate("2014-09-29T11:13:52.0Z")
}]