Couchdb document/function - mapreduce

In a document with the following information:
{
"address": [{
"Street": "123 xyz",
"City": "Belmont"
}]
}
How can I view the name of the cities. Is this correct:
function(doc) {
emit(doc.address.City,null);
}
It returns only null. I wanted to see the name "Belmont".
Any help with be appreciated.

In your data, address is an array, so it does not have a City property.
If you only have one address in your data:
{
"address": {
"Street": "123 xyz",
"City": "Belmont"
}
}
Getting /{database}/_design/{ddoc}/_view/{view} should return:
{"rows":[
{"key":"Belmont", "id":"{id}", "value":null}
]}
As a side note, please note that you can also get /{database}/_design/{ddoc}/_view/{view}?include_docs=true:
{"rows":[
{"key":"Belmont", "id":"{id}", "value":null, "doc":{
"address": {
"Street": "123 xyz",
"City": "Belmont"
}
}}
]}
Last but not least, if you really need multiple adresses in your data, you can send them all:
function(o) {
for each (var a in o.address) {
emit(a.City);
}
}

Related

How to find middle of the string, next to space and dot in MongoDB

[
{
"Name": "Dr.Soma",
"Email": "drsoma#gmail.com",
"MobNo": 111111111
},
{
"Name": "Bootha Ganesh",
"Email": "boothaganesg#gmail.com",
"MobNo": 222222222
},
{
"Name": "Steven",
"Email": "steven#gmail.com",
"MobNo": 333333333
},
{
"Name": "Dr.Anbarasi",
"Email": "anbarasi#gmail.com",
"MobNo": 4444444444
}
]
I try this to using find regex
db.details.find({Name:{$regex:/steven/i}})
output:
{
"Name": "Steven",
"Email": "steven#gmail.com",
"MobNo": 333333333
}
How to find data Name dot(.) after Soma & Space after Ganesh
Excepted Output
If I find Name Ganesh,I need
{
"Name": "Bootha Ganesh",
"Email": "boothaganesg#gmail.com",
"MobNo": 222222222
}
If I find Name small s or capital S ,I need
{
"Name": "Dr.Soma",
"Email": "drsoma#gmail.com",
"MobNo": 111111111
}
No Need Name Dr.Anbarasi data
db.collection.find({"Name": {'$regex': /\bsoma[a-zA-Z0-9]*/gi}})
\b assert position at a word boundary: (^\w | \w$ | \W\w|\w\W)
soma-for searching value
[a-zA-Z0-9]-word characters
*-to entire string
db.collection.find({ Name: { $regex: "Soma" } })
mongoplayground
db.collection.find({ Name: { $regex: ".Soma" } })
mongoplayground
db.collection.find({ Name: { $regex: " Ganesh" } })
mongoplayground

How to get the last createdAt item with TypeORM

Hi friends I need help with this query that I want to make to return only the last 'Case' by 'User'.
On my PostMan request I have my service that returns all the cases that the user have made.
users.services.ts
async findCasesbyUserId(id: number) {
return await this.usersRepository.findOne(id, { relations: ['cases']})
}
users.controller.ts
#Get('/findCase/:id')
async findCasesByUserId(#Param('id', ParseIntPipe) id: number,) {
return this.usersService.findCasesbyUserId(id)
}
When I made my GET request to 'http://localhost:3000/users/findCase/3'. This is the result that I get.
The user with id=3 have the cases with the id=1, id=2, id=3
So I want only to return only the case with the id=3 because is the last created case. I need a way to get the last case by the field createdAt or another way could be great for me.
Im using NestJs with TypeORM for thew querys to my PostgreSQL DB.
{
"id": 3,
"createdAt": "1617983079621",
"updatedAt": "1617983079621",
"name": "John",
"lastname": "Doe",
"email": "jdoe#email.com",
"username": "JohnDoe",
"password": "**************",
"role": "Admin",
"isActive": true,
"document": "12345",
"address": "Test",
"phone": "12345",
"cases": [
{
"id": 1,
"createdAt": "1618504530817",
"updatedAt": "1618504530817",
"message": "Test Case 1",
},
{
"id": 2,
"createdAt": "1618504530817",
"updatedAt": "1618504530817",
"message": "Test Case 1",
}, {
"id": 3,
"createdAt": "1618546146772",
"updatedAt": "1618546146772",
"message": "Test Case 1",
},
]
}

AWS StepFunctions - Merge and flatten the task output combined with the original input

How do we use Parameters, ResultPath and ResultSelector to combine the results of a Task with the original input in the same JSON level?
I checked the documentation on AWS, but it seems that ResultSelector always create a new dictionary which puts it in 1-level below on the result.
Example input
{
"status": "PENDING",
"uuid": "00000000-0000-0000-0000-000000000000",
"first_name": "John",
"last_name": "Doe",
"email": "john.doe#email.com",
"orders": [
{
"item_uuid": "11111111-1111-1111-1111-111111111111",
"quantities": 2,
"price": 2.38,
"created_at": 16049331038000
}
]
}
State Machine definition
"Review": {
"Type": "Task",
"Resource": "arn:aws:states:us-east-1:123456789012:activity:Review",
"ResultPath": null,
"Next": "Processing",
"Parameters": {
"task_name": "REVIEW_REQUIRED",
"uuid.$": "$.uuid"
}
},
Example output from Review Activity
{
"review_status": "APPROVED"
}
Question
How do I update the State Machine definition to combined the result of Review Activity and the original input to something as below?
{
"status": "PENDING",
"uuid": "00000000-0000-0000-0000-000000000000",
"first_name": "John",
"last_name": "Doe",
"email": "john.doe#email.com",
"orders": [
{
"item_uuid": "11111111-1111-1111-1111-111111111111",
"quantities": 2,
"price": 2.38,
"created_at": 16049331038000
}
],
"review_status": "APPROVED"
}
NOTE
I don't have access to the Activity code, just the definition file.
I recommend NOT doing the way suggested above as you will drop all data that you do not include. It's not a long term approach, you can more easily do it like this:
Step Input
{
"a": "a_value",
"b": "b_value",
"c": {
"c": "c_value"
}
}
In your state-machine.json
"Flatten And Keep All Other Keys": {
"Type": "Pass",
"InputPath": "$.c.c",
"ResultPath": "$.c",
"Next": "Some Other State"
}
Step Output
{
"a": "a_value",
"b": "b_value",
"c": "c_value"
}
While Step Function does not allow you to do so, you can create a Pass state that flattens the input as a workaround.
Example Input:
{
"name": "John Doe",
"lambdaResult": {
"age": "35",
"location": "Eastern Europe"
}
}
Amazon State Language:
"Flatten": {
"State": "Pass",
"Parameters": {
"name.$" : "$.name",
"age.$" : "$.lambdaResult.age",
"location.$": "$.lambdaResult.location"
},
"Next": "MyNextState"
}
Output:
{
"name": "John Doe",
"age": "35",
"location": "Eastern Europe"
}
It's tedious, but it gets the job done.
Thanks for your question.
It looks like you don't necessarily need to manipulate the output in any way, and are looking for a way to combine the state's output with its input before passing it on to the next state. The ResultPath field allows you to combine a task result with task input, or to select one of these. The path you provide to ResultPath controls what information passes to the output.

MongoDB - Find numbers that starts with a string

I'm trying to make a query that gets all the prices that starts with '12'.
I have a collection like this:
{
"place": "Costa Rica",
"name": "Villa Lapas",
"price": 1353,
},
{
"place": "Costa Rica",
"name": "Hotel NWS",
"price": 1948,
},
{
"place": "Costa Rica",
"name": "Hotel Papaya",
"price": 1283,
},
{
"place": "Costa Rica",
"name": "Hostal Serine",
"price": 1248,
},
And I want my results like this:
{
'prices': [
1248,
1283
]
}
I'm converting all the prices to string in order to use a regex function. But I don't understand very well how to use the regex in my query.
My query returns:
{ "prices" : null }
{ "prices" : null }
Could someone please guide me? :)
db.collection.aggregate([
{'$project': {
'_id': 0,
'price': {'$toString': '$price'}
}},
{'$project': {
'prices': {'$regexFind': { 'input': "$price", 'regex': '^12' }}
}}
]).pretty();
You are almost correct.
db.test.aggregate([
{'$project': {
'_id': 0,
'prices': {'$toString': '$price'}
^^^ -> I meant this
}},
{'$match': {
'prices': {'$regex': '^12' }
^^^ -> same here
}}
])
You need to use $match with $regex which yields the result as you expected.
If you use regexFind, it works on all matching docs and returns null where input doesn't match the pattern
And
In the first project you have price instead prices. If you refer the first project name in the second project, then pipeline matches.

AlpacaJS: programmatically change value of TextField after selecting Select

I'm new to AlpacaJS and getting crazy trying to figure out how to do a simple stuff like changing dinamically the content of a text field with the value of a "Select".
The code looks like
$("#form1").alpaca({
"data": {
"name": "Default"
},
"schema": {
"title": "What do you think of Alpaca?",
"type": "object",
"properties": {
"name": {
"type": "string",
"title": "Name"
},
"flavour":{
"type": "select",
"title": "Flavour",
"enum": ["vanilla", "chocolate", "coffee", "strawberry", "mint"]
}
}
},
"options": {
"helper": "Tell us what you think about Alpaca!",
"flavour": {
"type": "select",
"helper": "Select your flavour.",
"optionLabels": ["Vanilla", "Chocolate", "Coffee", "Strawberry", "Mint"]
}
}
},
"postRender": function(control) {
var flavour = control.childrenByPropertyId["flavour"];
var name = control.childrenByPropertyId["name"];
name.subscribe(flavour, function(val) {
alert("Val = " + val);
this.schema.data = val;
this.refresh();
});
}
});
I can see that the function in postRenderer is called (as I can see the alert with relevant value) but (maybe I'm brain dead at this stage) I cannot refresh the text field with that value.
Cheers
I was probably looking at the wrong attribute to be set... After I changed to
this.schema.data = val;
it worked fine :)