How to use couchdb _find request with a number of selectors? - javascript

I'm trying to get documents for different selectors in one _find request. How is this possible to implement?
"selector": {
"$or": [
{"author": "007177e3-6719-4237-8903-fb397977889f"},
{"writers": {"$regex": "007177e3-6719-4237-8903-fb397977889f"}},
{"readers": {"$regex": "007177e3-6719-4237-8903-fb397977889f"}}
]
},
"use_index": "_design/5e0c1e3422759aefaa2aa14ed5a8323ba33f06e3",
"sort": [{"title": "asc"}],
"fields": ["_id", "_rev", "id", "title","author", "writers", "readers"]
Index used in request
{
"ddoc": "_design/5e0c1e3422759aefaa2aa14ed5a8323ba33f06e3",
"name": "title-json-index",
"type": "json",
"partitioned": false,
"def": {
"fields": [
{
"title": "asc"
}
]
}
},
In the response I only get document there author is "007177e3-6719-4237-8903-fb397977889f". Other selectors (writers, readers) are ignored.
Doc sample:
{
"_id": "scheme-44b2b6f6-9b42-40b5-86c3-6135cde2f99f",
"_rev": "6-bcbd906fd316e098fdd45eda3c11008f",
"id": "scheme-44b2b6f6-9b42-40b5-86c3-6135cde2f99f",
"title": "New scheme 25.02.2022",
"scope": "meta",
"created": "2022-02-25T05:43:45.546Z",
"modified": "2022-03-04T04:09:25.659Z",
"sharedUI": {
"schemeSharingLock": false,
"schemeRenameLock": false,
"schemeDeleteLock": false
},
"author": "007177e3-6719-4237-8903-fb397977889f",
"writers": "fff,007177e3-6719-4237-8903-fb397977889f,ccc",
"readers": "yyyy",
}
CouchDB version: 3.2.0

Related

How to remove deeply nested object (Node.js, mongoose)

I'm making a kanban task management app and I'm trying to remove a task with the _id: req.params.id which has the value of 62fa5ae05778ec97bc6ee23a. I tried the following:
const task = await Board.findOneAndUpdate(
{
"columns.tasks._id": req.params.id,
},
{ $pull: { "columns.$.tasks.$._id": req.params.id } },
{ new: true }
);
But I get the error Too many positional (i.e. '$') elements found in path'columns.$.tasks.$._id'
I searched for a while and came across arrayFilters from the docs but I'm struggling a lot to understand how to implement it for this particular need.
{
"_id": "62fa5aa25778ec97bc6ee231",
"user": "62f0eb5ebebd0f236abcaf9d",
"name": "Marketing Plan",
"columns": [
{
"name": "todo",
"_id": "62fa5aa25778ec97bc6ee233",
"tasks": [
{
"title": "Task Four",
"description": "This is task four",
"subtasks": [
{
"name": "wash dshes",
"completed": false,
"_id": "62fa5ae05778ec97bc6ee23b"
},
{
"name": "do homework",
"completed": false,
"_id": "62fa5ae05778ec97bc6ee23c"
}
],
"_id": "62fa5ae05778ec97bc6ee23a"
}
]
},
{
"name": "doing",
"_id": "62fa5aa25778ec97bc6ee234",
"tasks": []
},
{
"name": "done",
"_id": "62fa5aa25778ec97bc6ee235",
"tasks": []
}
],
"__v": 0
}
You need to use $[] positional operator in order to pull from the nested array. Try running this query:
db.Board.updateOne({
"_id" : "62fa5aa25778ec97bc6ee231",
}, {
$pull: { 'columns.$[].tasks': { '_id': '62fa5ae05778ec97bc6ee23a' } }
});

Post request with array

I want to test my Web API with a postman on the javascript app.
I have the following swagger.
"/attachment/erase": {
"post": {
"summary": "Erase ",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "folder",
"in": "query",
"type": "string",
"description": "folder('s) detail",
"required": true
},
{
"name": "type",
"in": "query",
"type": "string",
"description": "ABC or BCD",
"required": true
},
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"type": "object",
"required": ["files"],
"properties": {
"attachments": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"description": "The file(s) to delete"
}
],
"responses": {
"200": {
"description": "file(s) erased."
}
}
}
}
So I send the following request :
with the following body :
In return, I get an error 415.
What I am doing wrong?
You're sending x-www-form-urlencoded when the API is expecting application/json.
How about passing a form-data parameter file, please refer the image

trouble while parsing json

I am trying to react displayName in req
when I write
console.log(req.session.passport.user._raw)
the output is:
{
"kind": "plus#person",
"etag": "\"ucaTEV-ZanNH5M3SCxYRM0QRw2Y/XiR7kPThRbzcIw-YLiARoF22TMY\"",
"emails": [
{
"value": "rajanlagah#gmail.com",
"type": "account"
}
],
"objectType": "person",
"id": "100428644453494579140",
"displayName": "Rajan Lagah",
"name": {
"familyName": "Lagah",
"givenName": "Rajan"
},
"url": "https://plus.google.com/100428644453494579140",
"image": {
"url": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50",
"isDefault": true
},
"isPlusUser": true,
"language": "en",
"circledByCount": 0,
"verified": false
}
Now the from this object I am trying to get kind (say)
console.log(req.session.passport.user._raw.kind)
then the output is undefined
Can any one tell my mistake?
Is req.session.passport.user._raw a string? You may need to parse it first, eg:
var data = JSON.parse(req.session.passport.user._raw);
console.log(data.kind);

How to insert values of matching JSON Keys between 2 JSON files

I have 2 JSON files:
A Template JSON
A JSON output (from awscli)
The template is a small JSON file as below:
{
"DryRun": true,
"ImageId": "",
"KeyName": "",
"SecurityGroups": [
""
],
"InstanceType": "",
"Monitoring": {
"Enabled": false
},
"SubnetId": "",
"DisableApiTermination": true,
"PrivateIpAddress": "",
"IamInstanceProfile": {
"Arn": "",
"Name": ""
},
"EbsOptimized": true,
"TagSpecifications": [{
"ResourceType": "",
"Tags": [{
"Key": "",
"Value": ""
}]
}]
}
The original file is the output of aws ec2 describe-instances:
{
"Reservations": [{
"OwnerId": "123456789012",
"ReservationId": "r-12345678",
"Groups": [],
"Instances": [{
"Monitoring": {
"State": "disabled"
},
"PublicDnsName": "ec2-12-34-56-78.ap-southeast-1.compute.amazonaws.com",
"RootDeviceType": "ebs",
"State": {
"Code": 16,
"Name": "running"
},
"EbsOptimized": false,
"LaunchTime": "2016-02-09T03:06:21.000Z",
"PublicIpAddress": "12.34.56.78",
"PrivateIpAddress": "172.31.1.2",
"ProductCodes": [],
"VpcId": "vpc-1a2b3c4d",
"StateTransitionReason": "",
"InstanceId": "i-abcd1234",
"ImageId": "ami-1234abcd",
"PrivateDnsName": "ip-172-31-1-2.ap-southeast-1.compute.internal",
"KeyName": "tempKey",
"SecurityGroups": [{
"GroupName": "somegroup1",
"GroupId": "sg-ZZZZZ"
},
{
"GroupName": "somegroup2",
"GroupId": "sg-YYYYY"
}
],
"ClientToken": "NutKc123456789012",
"SubnetId": "subnet-00001234",
"InstanceType": "t2.medium",
"NetworkInterfaces": [{
"Status": "in-use",
"MacAddress": "02:AA:BB:CC:DD:EE",
"SourceDestCheck": true,
"VpcId": "vpc-1a2b3c4d",
"Description": "",
"Association": {
"PublicIp": "12.34.56.78",
"PublicDnsName": "ec2-12-34-56-78.ap-southeast-1.compute.amazonaws.com",
"IpOwnerId": "123456789012"
},
"NetworkInterfaceId": "eni-XXXXXXXX",
"PrivateIpAddresses": [{
"PrivateDnsName": "ip-172-31-1-2.ap-southeast-1.compute.internal",
"Association": {
"PublicIp": "1.2.3.4",
"PublicDnsName": "ec2-12-34-56-78.ap-southeast-1.compute.amazonaws.com",
"IpOwnerId": "123456789012"
},
"Primary": true,
"PrivateIpAddress": "172.31.1.2"
}],
"PrivateDnsName": "ip-172-31-1-2.ap-southeast-1.compute.internal",
"Attachment": {
"Status": "attached",
"DeviceIndex": 0,
"DeleteOnTermination": true,
"AttachmentId": "eni-attach-XXXXXXXX",
"AttachTime": "2016-01-13T08:33:37.000Z"
},
"Groups": [{
"GroupName": "somegroup1",
"GroupId": "sg-ZZZZZZ"
},
{
"GroupName": "somegroup2",
"GroupId": "sg-YYYYYY"
}
],
"Ipv6Addresses": [],
"SubnetId": "subnet-00001234",
"OwnerId": "123456789012",
"PrivateIpAddress": "172.31.1.2"
}],
"SourceDestCheck": true,
"Placement": {
"Tenancy": "default",
"GroupName": "",
"AvailabilityZone": "ap-southeast-1b"
},
"Hypervisor": "xen",
"BlockDeviceMappings": [{
"DeviceName": "/dev/xvda",
"Ebs": {
"Status": "attached",
"DeleteOnTermination": true,
"VolumeId": "vol-33221100",
"AttachTime": "2016-01-13T08:33:39.000Z"
}
}],
"Architecture": "x86_64",
"StateReason": {
"Message": "Client.UserInitiatedShutdown: User initiated shutdown",
"Code": "Client.UserInitiatedShutdown"
},
"RootDeviceName": "/dev/xvda",
"VirtualizationType": "hvm",
"Tags": [{
"Value": "SomeValue",
"Key": "SomeKey"
},
{
"Value": "AnotherValue",
"Key": "Name"
}
],
"AmiLaunchIndex": 0
}]
}]
}
I want to copy the values of the Keys in the original JSON file to the template file.
For example, KeyName is a common key between the 2 JSON files. The corresponding value tempKey is replaced in the template file.
The main use case of this is: I am trying to migrate a number of servers on AWS from 1 region to another. This is a part of migration process which will remove tons of manual clicking and configuration on AWS Console.
Note: I use BASH command line.
There's a way to do this with jq but it doesn't take a JSON template as input.
You'll have to modify it to become a query. This isn't the actual query you need, but something to get you started:
cat temp.json | jq '.Reservations[].Instances[] | { DryRun, ImageId, KeyName, SecurityGroups, InstanceType, Monitoring }'
Where temp.json is your output above that I placed into a file. For regular commands, just do something like aws ec2 describe-instances | jq ...
The output that gives me (keep in mind the restricted set I queried for) is:
{
"DryRun": null,
"ImageId": "ami-1234abcd",
"KeyName": "tempKey",
"SecurityGroups": [
{
"GroupName": "somegroup1",
"GroupId": "sg-ZZZZZ"
},
{
"GroupName": "somegroup2",
"GroupId": "sg-YYYYY"
}
],
"InstanceType": "t2.medium",
"Monitoring": {
"State": "disabled"
}
}
Hope this helps.

Display data from common topic in Freebase using jQuery

I am playing around with Freebase and have had some decent success, but have hit a wall. My MQL is below. I do not have any issue displaying name,latin name, etc, which I created in my base. I do not know how to display the article which is in a different base.
Here is the jQuery I am using to display data:
$('<div>',{text:this.name}).appendTo(document.body);
Thank you very much,
Todd
query : [
{
"/common/topic/article": {
"guid": null,
"limit": 1,
"optional": true
},
"/common/topic/image": {
"id": null,
"limit": 1,
"optional": true
},
"id": null,
"larval_food": [
{
"index": null,
"lang": "/lang/en",
"limit": 6,
"optional": true,
"sort": "index",
"type": "/type/text",
"value": null
}
],
"latin_name": [
{
"index": null,
"lang": "/lang/en",
"limit": 6,
"optional": true,
"sort": "index",
"type": "/type/text",
"value": null
}
],
"limit": 60,
"name": null,
"s0:type": [
{
"id": "/base/butterflies/butterfly",
"link": [
{
"timestamp": [
{
"optional": true,
"type": "/type/datetime",
"value": null
}
],
"type": "/type/link"
}
],
"type": "/type/type"
}
],
"sort": "-s0:type.link.timestamp.value",
"type": "/base/butterflies/butterfly"
}
]
Change
"type": "/base/butterflies/butterfly"
to the type of the thing you actually want to include.
As an aside, that looks like a query which was exported from one of the Freebase.com view pages. It can be greatly simplified and some of the stuff, like the sorting, you probably want removed altogether.
Here's your query simplied (I also recommend using the standard scientific name property instead of inventing your own "Latin name" property):
[{
"type": "/base/butterflies/butterfly",
"mid": null,
"name": null,
"/common/topic/article": [],
"/common/topic/image": ["mid":null,"optional":true],
"larval_food": [],
"latin_name": [],
"/biology/organism_classification/scientific_name" : [],
}]
Here's a version of the query which shows all organism classifications (species in this case) which have the tribe Danaini two levels up. It optionally decorates it with data (larval_food) from your base, if it exists:
[{
"type": "/biology/organism_classification",
"higher_classification": [{
"/biology/organism_classification/higher_classification": "Danaini"
}],
"mid": null,
"name": null,
"scientific_name": [],
"/common/topic/article": [],
"/common/topic/image": [{
"mid": null,
"optional": true
}],
"/base/butterflies/butterfly/larval_food": [],
}]
You can try it here: http://tinyurl.com/6wht7lx

Categories

Resources