Converting a result to a list - list

I need your help to convert a result of a ServiceNow API request to a list.
The result shows like following:
"json": {
"result": {
"success": "True",
"entries": {
"matches": 119,
"records": [
{
"our_reference": "INV01567761"
},
{
"our_reference": "INV01134545"
},
{
"our_reference": "INV01345435"
},
{
"our_reference": "INV01345345"
}
]
What I need is just the value in the dict in a list form like:
["INV01567761","INV01567761","INV01567761","INV01567761"]
to get the list of the records field I choose:
- name: trying to get a list
set_fact:
incidentList: "{{ ticketresult.json.result.entries.records }}"
- debug: var=incidentList
output:
[
{
"our_reference": "INV01567761"
},
{
"our_reference": "INV01134545"
},
{
"our_reference": "INV01345435"
},
{
"our_reference": "INV01345345"
}
]
but then I'm lost....
I tried several things to extract the numbers from this dict, but getting always the error message because this dict is sitting in a list.
so I tried:
ticketresult.json.result.entries.records[0]
to get the dict in the list, but this returned only the first item in the dict:
{ "our_reference": "INV01567761" }
Is there an easy way to get out all the info in list-form?

The expression below
references: "{{ json.result.entries.records|
map(attribute='our_reference')|list }}"
gives the list
references:
- INV01567761
- INV01134545
- INV01345435
- INV01345345

I also found another way:
- set_fact:
list_of_tickets: "{{ list_of_tickets|default([]) + [item|string|regex_search('INV[0-9]{8}')] }}"
with_items:
- "{{ ticketresult.json.result.entries.records }}"

Related

monogdb full text search, ignore characters

Im implementing a mongodb search.
The search performs a find on field values:
[{
value: "my.string.here"
}, {
value: "my other here"
}{
...
}]
When i enter "my" both entries are found. What have my query to look like to ignore the dots on the first entry? So when i enter "my string" the first element gets returned?
Actually it works only when i enter "my.string" which is not nice.
let limit = Number(req.query.limit || 100);
let skip = Number(req.query.skip || 0);
collection.find({
$or: [{
value: new RegExp(req.body.search, "gi")
}, {
tags: {
$in: req.body.search.split(",").map((val) => {
return new RegExp(val, "gi")
})
}
}]
}).skip(skip).limit(limit).toArray((err, result) => {
if (err) {
res.status(500).json(err);
} else {
res.status(200).json(result);
}
});
EDIT:
A solution could look like this:
let query = {
$or: [{
name: new RegExp(req.body.search, "gi")
}, {
tags: {
$in: req.body.search.split(",").map((val) => {
return new RegExp(val, "gi")
})
}
}, {
name: new RegExp(req.body.search.split(' ').join('.'), "gi")
}, {
name: new RegExp(req.body.search.split(' ').join('_'), "gi")
}, {
name: new RegExp(req.body.search.split(' ').join('-'), "gi")
}]
};
But i find it ugly and not elegant. Is there a better way to do this ?

DynamoDB ProjectionExpression Set Contains

I have table with items which each contain a string set, as well as other information. Can I query the table and get back a flag if the set contains a particular entry. I don't want to filter by the set, just find out if it contains an item without pulling back the full set.
For example, say I have the following items:
[
{
things: { "a", "b" },
name: "Coffee"
},
{
things: { "b" },
name: "Tea"
}
]
I would like to query this table with something like Projection: "flag=(thing CONTAINS 'a'), name" and get back:
[
{
flag: true,
name: "Coffee"
},
{
flag: false,
name: "Tea"
}
]
Is this possible?

MongoDB search by tags with regular expression

Suppose I have model looks like:
{_id: ..., tags: ["stackoverflow", "github", "bootstrap", ...]}
How can I search for all items that match BOTH /stack/ and /git/ ?
There mongo regex doc
http://docs.mongodb.org/manual/reference/operator/query/regex/
db.collection.find(
{
$and :
[
{ tags: { $regex: /git/ } },
{ tags: { $regex: /boot/ } }
]
})
this solution may help you
Searching in MongoDB
Just thought this question/previous answer can be improved:
The first solution that comes to mind when approaching this problem is :
db.collection.find({ tags: { $regex: /git/ }, tags: { $regex: /stack/ }}
But this will result in finding tag items which match only 'stack', because mongo builds this query in way which results in the first criteria overridden by second criteria(as they have the same key 'tag').
Hence the correct solution would be :
db.collection.find(
{
$and :
[
{ tags: { $regex: /git/ } },
{ tags: { $regex: /stack/ } }
]
})
Which will find documents which match both the regex conditions 'git' and 'stack' although the syntax is a bit contrived.

couchdb - startkey endkey doesn't filter records with key arrays as expected

I have Couch-DB map-reduce view which outputs this;
{
rows: [
{
key: [
"2014-08-20",
2,
"registration"
],
value: 2
},
{
key: [
"2014-08-20",
2,
"search"
],
value: 3
},
{
key: [
"2014-08-21",
2,
"registration"
],
value: 3
},
{
key: [
"2014-08-21",
2,
"search"
],
value: 4
}
]
}
I need to query all the records that has between 2014-08-20 and 2014-08-21 Also the same time I need the integer value in the middle to be 2 and the last value to be "registration".
My curl request URL looks like this
BASE_URL?group=true&startkey=["2014-08-20",1,"registration"]
&endkey=["2014-08-21",1,"registration"]
This filters out the date but none of the other elements (the integer value and the string). Anyone got an idea whats happening?
Any help would be appreciated.
UPDATE
My Document structure looks something like this
[
{
platform_version: 2,
UDID: "EWHSJeNp20sBFuzqcorkKVVi",
session: {
timestamp: "2014-08-20T00:00:00.000Z",
session_id: "kOnNIhCNQ31LlkpEPQ7XnN1D",
ip: "202.150.213.66",
location: "1.30324,103.5498"
},
events: [
{
event_type: "search",
timestamp: "2014-08-21T00:00:00.000Z",
location: "1.30354,103.5301",
attributes: {
}
}
]
},
{
platform_version: 2,
UDID: "EWHSJeNp20sBFuzqcorkKVVi",
session: {
timestamp: "2014-08-21T00:00:00.000Z",
session_id: "kOnNIhCNQ31LlkpEPQ7XnN1D",
ip: "202.150.213.66",
location: "1.30324,103.5498"
},
events: [
{
event_type: "search",
timestamp: "2014-08-21T00:00:00.000Z",
location: "1.30354,103.5301",
attributes: {
}
}
]
},
{
platform_version: 2,
UDID: "EWHSJeNp20sBFuzqcorkKVVi",
session: {
timestamp: "2014-08-20T00:00:00.000Z",
session_id: "kOnNIhCNQ31LlkpEPQ7XnN1D",
ip: "202.150.213.66",
location: "1.30324,103.5498"
},
events: [
{
event_type: "click",
timestamp: "2014-08-21T00:00:00.000Z",
location: "1.30354,103.5301",
attributes: {
}
}
]
}
]
and the map reduce function looks like this.
function(doc) {
date = doc.session.timestamp.split("T")[0];
eventArray = doc.events;
for (i = 0; i < eventArray.length; i++) {
emit([doc.app_version,eventArray[i].event_type,date],1);
}
}
It started working after I change the order of the keys. but still, I can't use a wildcard to query all the event types.
You are getting documents with different second array item different then 1 because in the CouchDB the limits (startkey and endkey) are compared to map keys using lexicographical order. In the lexicographical order [1,1] < [1,2] < [2,1] < [2,2].
What you need is either multidimensional queries (which are not supported by CouchDB), additional client-side filtering (which may increase data transferred between CouchDB and your app) or additional server-side filtering with list function (which increase processing time of queries).
If your app needs filtering using range only on the first element of key array (just like in your example query), you would solve your problem easily by placing the item at the last position of array, eg. emitting [1, "registration", "2014-08-21"] instead of ["2014-08-21", 1, "registration"].

What is the format expected by a find(id) request?

My backend replies to find all requests:
User.find();
Like this
{ 'users' : [ user1_obj, user2_obj ] }
Ember-data is happy about it. Now if I do a simple single object find:
User.find('user1');
I have tried configuring the backend to return any of the following:
user1
{ 'user1' : user1_obj }
{ 'user' : { 'user1' : user1_obj } }
{ 'user' : user1_obj }
But none of those are working. What should I return from the backend in reply to find("obj-id") requests? According to the documentation about JSON ROOT, the right format looks like:
{ 'user' : user1_obj }
Ember does not complain about it, but the Ember Objects processed have a very strange structure, like this:
As you can see, _reference.record is referring to the top record. Also (not shown here) _data field is empty.
What could be causing that strange nesting?
EDIT
As linked by mavilein in his answer, the JSON API suggests using a different format for singular resources:
{ 'users' : [user1_obj] }
That means, the same format as for plural resources. Not sure if Ember will swallow that, I'll check now.
Following this specification, i would suspect the following:
{
'users' : [{
"id": "1",
"name" : "John Doe"
},{
"id": "2",
"name" : "Jane Doe"
}]
}
For singular resources the specification says:
Singular resources are represented as JSON objects. However, they are
still wrapped inside an array:
{
'users' : [{
"id": "1",
"name" : "John Doe"
}]
}
Using User.find() will expect the rootKey pluralized and in your content an array of elements, the response format is the following json:
{
users: [
{ id: 1, name: 'Kris' },
{ id: 2, name: 'Luke' },
{ id: 3, name: 'Formerly Alex' }
]
}
And with User.find(1) the rootKey in singular, and just one object:
{
user: {
id: 1, name: 'Kris'
}
}
Here a demo showing this working