How to apply a pagination on array of objects in couchdb using hyperledger fabric? - couchdb

{
"_id": "usq",
"_rev": "5-f8e9a8853b15f0270df94c1ae71323216",
"transactions": [
{
"admin_notification": [],
"admin_status": "pending",
"payment_amount_usd": "1",
"sp_tx_datetime": "Feb 26, 2021, 12:22 PM",
"sp_tx_hash": "pi_tx1",
"sp_tx_status": "succeeded",
"sp_tx_toAddress": "Admin",
"tx_admin_dateTime": "-",
"user_buyplan_days": "7 Days"
},
{
"admin_notification": [],
"admin_status": "pending",
"payment_amount_usd": "2",
"sp_tx_datetime": "Feb 26, 2021, 4:09 PM",
"sp_tx_hash": "pi_tx2",
"sp_tx_status": "succeeded",
"sp_tx_toAddress": "Admin",
"tx_admin_dateTime": "-",
"user_buyplan_days": "7 Days"
},
{
"admin_notification": [],
"admin_status": "pending",
"payment_amount_usd": "1",
"sp_tx_datetime": "Feb 26, 2021, 12:22 PM",
"sp_tx_hash": "pi_tx3",
"sp_tx_status": "succeeded",
"sp_tx_toAddress": "Admin",
"tx_admin_dateTime": "-",
"user_buyplan_days": "7 Days"
}
],
"user_email": "s#mail.com",
"user_fName": "Sam",
"user_id": "user_2304354",
"user_lName": "Smith",
"user_password": "Abc#123456",
"user_type": "user",
"~version": "CgMBFgA="
}
Here I want only 2 transactions at the first time than the next ones. So I have used getQueryResultWithPagination methods But It doesn't work on a single object. So I created a view of CouchDB.
"views": {
"tx-view": {
"map": "function (doc) {if(doc.transactions.length > 0) { emit(doc.transactions); }}"
},
"tx-view-2": {
"map": "function (doc) { if(doc.transactions.length > 0) { doc.transactions.forEach(function (tag) { emit(doc.user_id, tag); });}}"
}
},
Can I add this view into chaincode query method and create a transaction for the same?
How do I resolve this?

Have you tried looking at this sample contract & application?
https://github.com/hyperledger/fabric-samples/tree/master/asset-transfer-ledger-queries

Related

How to query a property with a dash in CosmosDB SqlAPI?

Query: I wanted to get the list of records for all people born in the month of March.
{
"details": {
"state": "CA",
"city": "San Fransisco",
"date-of-birth": { // there is a "-" in the key
"month": "March",
"year": "2000"
}
},
"personId": "person1",
"id": "id1"
},
{
"details": {
"state": "CA",
"city": "San Jose",
"date-of-birth": { // there is a "-" in the key
"month": "April",
"year": "2000"
}
},
"personId": "person2"
"id": "id2"
}
I was hoping that the SQL Query would be like this, but got an error :
select * from c where c.details['date-of-birth'['month']] = "March"
Can someone help me out with the query? I did try to look at the docs but got a little bit confused.
Try this
select * from c where c.details['date-of-birth'].month = "March"

Issue on converting EnrichedActivity to String

I need to convert EnrichedActivity to String as I use stream-java:3.2.4 in a Flutter application.
I get EnrichedActivity list like this:
val list : List<EnrichedActivity> = client.flatFeed("user").getEnrichedActivities(EnrichmentFlags()
.withOwnReactions()
.withRecentReactions()
.withReactionCounts()).get()
Then I try to convert the list like this:
val resultString = ObjectMapper()
.setSerializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include.ALWAYS)
.writeValueAsString(list)
But some fields are erased by FasterXml-jackson (v2.9.6).
Isn't this the correct way to convert an EnrichedActivity list to String?
Note 1- Original EnrichedActivity list:
0 = {EnrichedActivity#15673}
"EnrichedActivity{id=5e02d656-27ef-11eb-88f9-128a130028af,
actor=Data{id=RqO5ns7mrYfvain3ETQpdPgIQZz1,
data={updated_at=2020-11-11T22:46:38.568116Z,
name=RqO5ns7mrYfvain3ETQpdPgIQZz1,
created_at=2020-11-11T22:46:38.568116Z}}, verb=post,
object=Data{id=b0e249e9-8695-4161-bb24-d3980da4cdeb, data={}},
foreignID=, target=Data{id=, data={}}, time=Mon Nov 16 12:37:39
GMT+03:00 2020, origin=null, to=null, score=null,
ownReactions={like=[Reaction{id=4186ea14-f3e5-4368-8a91-eafc12e3045b,
appID=null, kind=like, userID=RqO5ns7mrYfvain3ETQpdPgIQZz1,
activityID=5e02d656-27ef-11eb-88f9-128a130028af, parent=,
ownChildren=null, latestChildren={}, childrenCounts={},
userData=Data{id=RqO5ns7mrYfvain3ETQpdPgIQZz1,
data={updated_at=2020-11-11T22:46:38.568116Z,
name=RqO5ns7mrYfvain3ETQpdPgIQZz1,
created_at=2020-11-11T22:46:38.568116Z}}, activityData={},
extra={updated_at=2020-11-16T09:39:25.088760Z,
created_at=2020-11-16T09:39:25.088760Z}}]},
latestReactions={like=[Reaction{id=4186ea14-f3e5-4368-8a91-eafc12"
1= {EnrichedActivity#15674}
"EnrichedActivity{id=6f18988b-277d-11eb-96e2-1254437355c5,
actor=Data{id=RqO5ns7mrYfvain3ETQpdPgIQZz1,
data={updated_at=2020-11-11T22:46:38.568116Z,
name=RqO5ns7mrYfvain3ETQpdPgIQZz1,
created_at=2020-11-11T22:46:38.568116Z}}, verb=post,
object=Data{id=11f22b84-5168-452e-b098-c9f60818d6b2, data={}},
foreignID=, target=Data{id=, data={}}, time=Sun Nov 15 23:02:05
GMT+03:00 2020, origin=null, to=null, score=null,
ownReactions={like=[Reaction{id=63471022-3e44-4d44-ad3d-6104d96a0843,
appID=null, kind=like, userID=RqO5ns7mrYfvain3ETQpdPgIQZz1,
activityID=6f18988b-277d-11eb-96e2-1254437355c5, parent=,
ownChildren=null, latestChildren={}, childrenCounts={},
userData=Data{id=RqO5ns7mrYfvain3ETQpdPgIQZz1,
data={updated_at=2020-11-11T22:46:38.568116Z,
name=RqO5ns7mrYfvain3ETQpdPgIQZz1,
created_at=2020-11-11T22:46:38.568116Z}}, activityData={},
extra={updated_at=2020-11-16T09:31:16.925141Z,
created_at=2020-11-16T09:31:16.925141Z}},
Reaction{id=9c7e0401-6ed0-47a8-b239-2669786e3340, appID=null, kind="
Note 2- The serialized form of the list above:
[ { "actor": { "id": "RqO5ns7mrYfvain3ETQpdPgIQZz1", "updated_at":
"2020-11-11T22:46:38.568116Z", "name": "RqO5ns7mrYfvain3ETQpdPgIQZz1",
"created_at": "2020-11-11T22:46:38.568116Z" }, "id":
"5e02d656-27ef-11eb-88f9-128a130028af", "object": { "id":
"b0e249e9-8695-4161-bb24-d3980da4cdeb" }, "target": { "id": "" },
"time": "2020-11-16T09:37:39.284", "verb": "post", "foreign_id": "",
"message": "test2", "latest_reactions_extra": { "like": { "next": "" }
} }, { "actor": { "id": "RqO5ns7mrYfvain3ETQpdPgIQZz1", "updated_at":
"2020-11-11T22:46:38.568116Z", "name": "RqO5ns7mrYfvain3ETQpdPgIQZz1",
"created_at": "2020-11-11T22:46:38.568116Z" }, "id":
"6f18988b-277d-11eb-96e2-1254437355c5", "object": { "id":
"11f22b84-5168-452e-b098-c9f60818d6b2" }, "target": { "id": "" },
"time": "2020-11-15T20:02:05.321", "verb": "post", "foreign_id": "",
"message": "Test", "latest_reactions_extra": { "like": { "next":
"https://stream-io-api.com/api/v1.0/reaction/activity_id/6f18988b-277d-11eb-96e2-1254437355c5/like/?id_lt=4fa212db-0125-4b4e-9a77-04e4940ec64a&limit=5&withOwnChildren=false"
} } } ]

Flter mongodb database using mongoose nodejs

I need to filter some users according to some fixed criteria. I have a user collection and a talent collection. The talent collection holds the reference to a master category collection.
What I need is to filter these users according to the category in the talent collection and some keys from the user collection.
For example I need to search for a user whose gender is 'male' and education 'BTech' and will have talents as a programmer and tester
my user collection is like,
{
"_id": "5f1939239bd35429ac9cd78f",
"isOtpVerified": "false",
"role": "user",
"adminApproved": 1,
"status": 0,
"languages": "Malayalam, Tamil, Telugu, Kannada",
"name": "Test user",
"email": "test#email.com",
"phone": "1234567890",
"otp": "480623",
"uid": 100015,
"bio": "Short description from user",
"dob": "1951-09-07T00:00:00.000Z",
"gender": "Male",
"education": "Btech",
"bodyType": "",
"complexion": "",
"height": "",
"weight": "",
"requests": [],
"location": {
"place": "place",
"state": "state",
"country": "country"
},
"image": {
"avatar": "5f1939239bd35429ac9cd78f_avatar.jpeg",
"fullsize": "5f1939239bd35429ac9cd78f_fullsize.png",
"head_shot": "5f1939239bd35429ac9cd78f_head_shot.jpeg",
"left_profile": "5f1939239bd35429ac9cd78f_left_profile.png",
"right_profile": "5f1939239bd35429ac9cd78f_right_profile.png"
},
"__v": 42,
"createdAt": "2020-07-23T07:15:47.387Z",
"updatedAt": "2020-08-18T18:54:22.272Z",
}
Talent collection
[
{
"_id": "5f38efef179aca47a0089667",
"userId": "5f1939239bd35429ac9cd78f",
"level": "5",
"chars": {
"type": "Fresher",
},
"category": "5f19357b50bcf9158c6be572",
"media": [],
"createdAt": "2020-08-16T08:35:59.692Z",
"updatedAt": "2020-08-16T08:35:59.692Z",
"__v": 0
},
{
"_id": "5f3b7e6f7e322948ace30a2c",
"userId": "5f1939239bd35429ac9cd78f",
"level": "3",
"chars": {
"type": "Fresher",
},
"category": "5f19359250bcf9158c6be573",
"media": [
{
"adminApproved": 0,
"status": 0,
"_id": "5f3c22573065f84a48e04a14",
"file": "id=5f1939239bd35429ac9cd78f&dir=test&img=5f1939239bd35429ac9cd78f_image_undefined.jpeg",
"description": "test",
"fileType": "image",
"caption": "test file"
},
{
"adminApproved": 0,
"status": 0,
"_id": "5f3c2d7a8c7f8336b0bfced2",
"file": "id=5f1939239bd35429ac9cd78f&dir=test&img=5f1939239bd35429ac9cd78f_image_1.jpeg",
"description": "this is a demo poster for testing",
"fileType": "image",
"caption": "A Test Poster"
}
],
"createdAt": "2020-08-18T07:08:31.532Z",
"updatedAt": "2020-08-18T19:35:22.899Z",
"__v": 2
}
]
And the category in the above document is a separate one populated to this. the category collection as,
[
{
"_id": "5f19359250bcf9158c6be573",
"status": true,
"title": "Testing",
"description": "Application tester",
"code": "test",
"characteristics": [],
"createdAt": "2020-07-23T07:00:34.221Z",
"updatedAt": "2020-07-23T07:00:34.221Z",
"__v": 0
},
{
"status": true,
"_id": "5f29829a705b4e648c28bc88",
"title": "Designer",
"description": "UI UX Designer",
"code": "uiux",
"createdAt": "2020-08-04T15:45:30.125Z",
"updatedAt": "2020-08-04T15:45:30.125Z",
"__v": 0
},
{
"_id": "5f19357b50bcf9158c6be572",
"status": true,
"title": "programming",
"description": "Java programmer",
"code": "program",
"createdAt": "2020-07-23T07:00:11.137Z",
"updatedAt": "2020-07-23T07:00:11.137Z",
"__v": 0
}
]
So my filter terms will be;
{
categories: ["5f19359250bcf9158c6be573", "5f19357b50bcf9158c6be572"],
minAge: 18,
maxAge: 25,
minHeight: 5,
maxHeight: 6,
minWeight: 50,
maxWeight: 80,
complexion: "white",
gender: "male",
}
And the expected result will be a user have both the above talents and followed conditions,
{
users: { ..User details.. },
medias: { ...medias from the matching talents.. }
}
If there are two collections you need to join them either by primary key or _id with foriegn fields and you can use $lookup with $match to filter down.
Documentation
You need to use $lookup with pipeline,
$match you condition for category match
$lookup to join users collection
$match conditions for users collections fields
$match exclude documents that don't found matching users of criteria passed in conditions
db.talents.aggregate([
{
$match: {
category: { $in: ["5f19359250bcf9158c6be573", "5f19357b50bcf9158c6be572"] }
}
},
{
$lookup: {
from: "users",
as: "users",
let: { userId: "$userId" },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ["$$userId", "$_id"] },
{ $eq: ["$gender", "Male"] },
{ $eq: ["$education", "Btech"] }
// ... add you other match criteria here
]
}
}
}
]
}
},
{ $match: { users: { $ne: [] } } }
])
Playground

convert to asc order of array data using MongoDB

how to make this data to asc order by user's first name and user's last name.
I got the response, want to sort the records by user's first name but it is taking from creation date I guess when using sort how can I make it base on user's first name and user's last name please guide
{
"response": {
"items": [
{
"_id": "5e71f86bd300b313df52fb2f",
"last_message": {
"text": "Alex",
"users": [
{
"_id": "5e4a8d2d3952132a08ae5764",
"first_name": "zack",
"last_name": "Write"
}
]
},
"texter": [
"alex",
"$0ctg"
],
"title": "New group1",
"group": true,
"members": [
{
"_id": "5e4a8afc3952132a08ae575e",
"first_name": "test3",
"last_name": "test4"
}
],
"managers": [
"5e4a8afc3952132a08ae575e"
],
"member": {
"_id": "5e4a8afc3952132a08ae575e",
"first_name": "test3",
"last_name": "test4"
}
},
{
"_id": "5e4e740f380054797d9db621",
"last_message": {
"text": "",
"users": [
{
"_id": "5e4a8d2d3952132a08ae5764",
"first_name": "yuhan",
"last_name": "jacob"
}
]
},
"texter": [
"",
"",
"",
"",
"",
"new iphone x\n brand new iphone wv wwvqzwzvq had sqswgscq wow you wholeheartedly q \n $600.00",
"helo",
"hello",
"hi"
],
"members": [
{
"_id": "5e4d0973babf2b74ca868f4d",
"first_name": "alex",
"last_name": "hales"
}
],
"managers": [],
"member": {
"_id": "5e4d0973babf2b74ca868f4d",
"first_name": "alex",
"last_name": "hales"
}
}
]
}
}
Tried
{
$sort: {
users: 1,
}
},
doesn't help much
Also if I would like to add two field asc desc order what would be the process in MongoDB
Try this hope this will help you !
{
$sort: { "users.first_name": 1 }
},

Populating all items from ref model [duplicate]

This question already has answers here:
$lookup multiple levels without $unwind?
(1 answer)
Mongoose: deep population (populate a populated field)
(10 answers)
Closed 3 years ago.
I have a database structured like this:
I want to get all listItems, of all lists from a user with a x ID. What's the correct way of doing that?
I'm using node with mongoose, and I tried the following:
await User.findById(user._id).populate('list');
But realized that I can't populate all ListItem's from that. Meaning, I can't do this:
await User.findById(user._id).populate('list').populate('listItem');
How can I get all listItems, of all lists from a user with a x ID?
Assuming User, List, and ListItem are Collections, you should be able to accomplish this using $lookup..
Here is a live demo of the following query..
Query:
db.users.aggregate([
{
$match: {
uniqueId: 1
}
},
{
$lookup: {
from: "lists",
localField: "uniqueId",
foreignField: "userId",
as: "lists"
}
},
{
$lookup: {
from: "listItems",
localField: "uniqueId",
foreignField: "userId",
as: "listItems"
}
}
])
Dataset:
db={ // Simulates a DB ********
"users": [ // Simulates a Collection ********
{
"firstname": "John",
"lastname": "Smith",
"email": "jsmith#gmail.com",
"password": "password123",
"uniqueId": 1
},
{
"firstname": "Jane",
"lastname": "Doe",
"email": "doe#yahoo.com",
"password": "123password",
"uniqueId": 2
}
],
"lists": [ // Simulates a Collection ********
{
"userId": 1,
"name": "Johns List 1",
"items": [
11,
12,
13
]
},
{
"userId": 2,
"name": "Groceries",
"items": [
21,
22,
23
]
}
],
"listItems": [ // Simulates a Collection ********
{
"userId": 2,
"itemId": 21,
"title": "Apple",
"notes": []
},
{
"userId": 2,
"itemId": 22,
"title": "Banana",
"notes": []
},
{
"userId": 2,
"itemId": 23,
"title": "Strawberry",
"notes": []
},
{
"userId": 1,
"itemId": 11,
"title": "Oil Change",
"notes": []
},
{
"userId": 1,
"itemId": 12,
"title": "Pick Up Grandma",
"notes": []
},
{
"userId": 1,
"itemId": 13,
"title": "Go For Run",
"notes": []
}
]
}
Result:
[
{
"_id": ObjectId("5a934e000102030405000008"),
"email": "jsmith#gmail.com",
"firstname": "John",
"lastname": "Smith",
"listItems": [
{
"_id": ObjectId("5a934e000102030405000003"),
"itemId": 11,
"notes": [],
"title": "Oil Change",
"userId": 1
},
{
"_id": ObjectId("5a934e000102030405000004"),
"itemId": 12,
"notes": [],
"title": "Pick Up Grandma",
"userId": 1
},
{
"_id": ObjectId("5a934e000102030405000005"),
"itemId": 13,
"notes": [],
"title": "Go For Run",
"userId": 1
}
],
"lists": [
{
"_id": ObjectId("5a934e000102030405000006"),
"items": [
11,
12,
13
],
"name": "Johns List 1",
"userId": 1
}
],
"password": "password123",
"uniqueId": 1
}
]

Resources