Couchbase N1QL: Update on nested Documents - couchbase

When having a nested document like this:
{
"blog": "testblog1",
"user_id": 41,
"comments": [
{
"comment": "testcomment1",
"user_id": 883
},
{
"comment": "testcomment2",
"user_id": 790
}
]
}
can one do an Update Operation with N1QL to add an extra field to a subdocument?
I would like to add the field "ranking" : "top comment" to the subdocument with the comment testcomment2:
{
"blog": "testblog1",
"user_id": 41,
"comments": [
{
"comment": "testcomment1",
"user_id": 883
},
{
"comment": "testcomment2",
"user_id": 790,
"ranking" : "top comment"
}
]
}
Site Note: The following statement would add a field to the root document on the Collection Blog:
UPDATE Blog SET rank = "top blog" WHERE blog = "testblog1";

Yes, you can do the following in N1QL:
UPDATE Blog
SET c.ranking = "top comment" FOR c IN comments WHEN c.comment = "testcomment2" END
WHERE blog = "testblog1"

Related

How to multiple update a nested array within document at couchbase?

I have a bucket called chat which includes document model like
{
"id": "50542400778551298_5001_abcdef",
"customer": {
...
},
"seller": {
...
},
"complaints": [],
"messages": [
{
"id": "2828911d-b96f-4b90-a52c-252d0324ab69",
"owner": "SELLER",
"content": "Merhabalar",
"readingDate": null,
"createdAt": "2022-06-27T09:16:42.49158+03:00"
},
{
"id": "8cd8c4b7-d599-4dfa-9fa7-be304a3b72b9",
"owner": "SELLER",
"content": "İlan hakkında detay bilgi verir misin?",
"readingDate": null,
"createdAt": "2022-06-27T09:17:43.329741+03:00"
},
{
"id": "4f0ae912-7d65-4e79-9058-2b048c4d9845",
"owner": "SELLER",
"content": "Huhuu",
"readingDate": null,
"createdDate": "2022-06-27T09:18:37.491531+03:00"
},
{
"id": "d2dd61b3-7ff1-4139-a411-9a917c00d4f3",
"owner": "SELLER",
"content": "Orada kimse var mı?",
"readingDate": null,
"createdAt": "2022-06-27T09:18:45.917564+03:00"
},
{
"id": "ea2d8dca-2f8c-4987-9c07-7515a6a0063f",
"owner": "SELLER",
"content": "Döner misininiz?",
"readingDate": null,
"createdAt": "2022-06-27T09:18:52.80337+03:00"
}
],
"status": "ACTIVE"
}
I want to update all messages readingDate a specific time like (2022-06-27T09:18:52.80337+03:00)
But I cannot find an easy way to accomplish this task.
I tried to write an N1QL like
UPDATE c.messages SET c.messages.readingDate = NOW_LOCAL()
FROM chat AS c
WHERE META().id = '50542400778551298_5001_abcdef';
but it is not working and gives error
[
{
"code": 3000,
"msg": "syntax error - at SET",
"query": "UPDATE c.messages SET c.messages.readingDate = NOW_LOCAL()\nFROM chat AS c \nWHERE META().id = '50542400778551298_5001_abcdef';"
}
]
I saw https://docs.couchbase.com/server/current/guides/bulk-operations.html.
As far as to my understanding, the general solution gets all messages and updates them based on cas.
But maybe I believe there is an easy way so I wanted to ask the StackOverflow community.
Thanks to this question I wrote n1ql like
UPDATE chat USE KEYS '50542400778551298_5001_abcdef'
SET c.readAt = NOW_LOCAL() FOR c IN messages END
RETURNING messages;

sub document under main document how to get a listing with pagination using couchbase(N1QL) query

any one can help me how to get the sub document List with pagination
i just give a sample example :
{
"accessories": [`
{
"data": {
"name": "TEST",
"updated_at": "2020-03-27T16:16:20.818Z"
},
"id": "56e83ea1-042e-47e0-85f8-186189c37426"
}
],
"calibration_reports": [`
{
"data": {
"deleted_at": "",
"frm27_equipment": [
"test_cat1"
],
"frm27_link": [
"yes"
],
"frm27_submit": null,
"updated_at": "2020-03-30T10:24:52.703Z"
},
"id": "e4c8b1b4-7f37-46db-a49d-bca74482b968"
},
{
"data": {
"deleted_at": "",
"frm27_equipment": [
"test_cat1"
],
"frm27_link": [
"no"
],
"frm27_submit": null,
"updated_at": "2020-03-30T10:34:37.615Z"
},
"id": "445854d6-66bf-4e33-b620-05a5053119a8"
}
],
}
]
}
Here i want to get a calibration_reports list with pagination is it possible ? using couchbase (N1ql Query)
please if any one know, what is the process how to get the list of result with pagination using couchbase(N1QL) query. please help me.
One possible way to go about this is to use UNNEST.
For instance:
SELECT calreports.id
FROM utpal u
UNNEST u.calibration_reports calreports
This would return something like:
[
{ "id": "aaa" },
{ "id": "bbb" },
{ "id": "ccc" },
... etc ...
]
And then you can use normal LIMIT/OFFSET for pagination, like so:
SELECT calreports.id
FROM utpal u
UNNEST u.calibration_reports calreports
LIMIT 50
OFFSET 150;

finding document in mongodb with specific id and username

{
"data": [
{
"_id": 555,
"username": "jackson",
"status": "i am coding",
"comments": [
{
"user": "bob",
"comment": "bob me "
},
{
"user": "daniel",
"comment": "bob the builder"
},
{
"user": "jesus",
"comment": "bob the builder"
},
{
"user": "hunter",
"comment": "bob the builder"
},
{
"user": "jeo",
"comment": "bob the builder"
},
{
"user": "jill",
"comment": "bob the builder"
}
]
}
]
}
so i want to get the result with _id :555 and user:bob i tried with below code but i cant make it work it returns empty array
app.get('/all',function(req , res){
db.facebook.find({_id:555},{comments:[{user:"bob"}]},function(err,docs){res.send(docs,{data:docs});});
} );
i want the result to be like this listed below with the comment with user:bob
{
"_id": 555,
"username": "jackson",
"status": "i am coding",
"comments": [
{
"user": "bob",
"comment": "bob me "
}
]
}
Only aggregate or mapReduce could exclude items from subarray in output. Shortest is to use $redact:
db.facebook.aggregate({
$redact:{
$cond:{
if:{$and:[{$not:"$username"},{$ne:["$user","bob"]}]},
then: "$$PRUNE",
else: "$$DESCEND"
}
}
})
Explanation:
$reduct would be applied to each subdocument starting from whole document. For each subdocument $reduct would either prune it or descend. We want to keep top level document, that is why we have {$not:"$username"} condition. It prevents top level document from pruning. On next level we have comments array. $prune would apply condition to each item of comments array. First condition {$not:"$username"} is true for all comments, and second condition {$ne:["$user","bob"]} is true for all subdocuments where user!="bob", so such documents would be pruned.
Update: in node.js with mongodb native driver
db.facebook.aggregate([{
$redact:{
$cond:{
if:{$and:[{$not:"$username"},{$ne:["$user","bob"]}]},
then: "$$PRUNE",
else: "$$DESCEND"
}
}
}], function(err,docs){})
One more thing: $prune is new operator and available only in MongoDB 2.6

Django + rest-framework: serialize arbitrary query

I have three classes representing different sections in my models: SectionA, SectionB, SectionC.
Each of these sections have associated a set of items (class Item in my model).
I would like to get a json similar to this:
{
"sectionA": [
{
"id": 1,
"picture": "car_pic1",
"category": "cat1"
},
{
"id": 3,
"picture": "car_pic1",
"category": "cat2"
},
{
"id": 5,
"picture": "car_pic1",
"category": "cat3"
}
],
"sectionB": [
{
"id": 2,
"picture": "car_pic1",
"category": "cat8"
},
{
"id": 4,
"picture": "car_pic1",
"category": "cat9"
},
{
"id": 7,
"picture": "car_pic1",
"category": "cat10"
},
],
"sectionC": [
{
"id": 9,
"picture": "car_pic1",
"category": "cat9"
},
{
"id": 10,
"picture": "car_pic1",
"category": "cat9"
},
{
"id": 11,
"picture": "car_pic1",
"category": "cat10"
},
]
}
This json displays any three items associated to each section.
I would like to know how can I implement this using rest-framework. Basically I need to perform a query retrieving the three items for each section (since this json is not associated to a model object) and serialize all this into the json. I'm not sure where or how to perform these queries and I didn't have any success so far.
Finally I did it slightly different. My view just creates a dictionary with each section and its associated items:
class SectionList(APIView):
"""
List three objects for each section.
"""
def generate_data(self):
#query to get the items of each section
list_items = []
list_items.append({"section" : "sectionA", "items" : secA_items})
list_items.append({"section" : "sectionB", "items" : secB_items})
list_items.append({"section" : "sectionC", "items" : secC_items})
return list_items;
def get(self, request, format=None):
section_list = self.generate_data()
serializer = SectionSerializer(section_list)
return Response(serializer.data)
And this is the serializer I used:
class SectionSerializer(serializers.Serializer):
section = serializers.CharField(max_length=200)
items = ItemSerializer(many=True)

Backbone.js Collections

I am working with backbone.js. I am trying to send a request to restful service i am getting the resultset as json object as shown
{
"Msgs": [
"Alert",
"Not"
],
"MessageStatus": [
"Active",
"Inactive"
],
"date": {
"From": "2013-04-25",
"To": "2013-06-25"
},
"Mlist": {
"Status": "PND",
"Role": "Admin,User",
"To": "2013-06-24",
"Id": 6,
"Datecreated": "2013-06-24",
"Title": "Title5",
"From": "2013-06-20"
}
}.
I am putting the json object extracting and setting it to collection but I am not able to get particular model from the collection with specific id.
If you want to make your model can be identified with id. You have to set the id in the attributes hash:
{
"id": 1001,
"Msgs": [
"Alert",
"Not"
],
"MessageStatus": [
"Active",
"Inactive"
],
"date": {
"From": "2013-04-25",
"To": "2013-06-25"
},
"Mlist": {
"Status": "PND",
"Role": "Admin,User",
"To": "2013-06-24",
"Id": 6,
"Datecreated": "2013-06-24",
"Title": "Title5",
"From": "2013-06-20"
}
}
Then using backbone collection "findWhere" method to get the specific id model.
//assume msgCollection is which you put the models
var model = msgCollection.findWhere({id: 1001});
Hope this is helpful for you.
I think you need to set your model and parse your input data. Checkout http://backbonejs.org/#Collection-model, http://backbonejs.org/#Model-parse and http://backbonejs.org/#Collection-parse.