how remove item with attrib repeat rethinkdb - reduce

i have a problem. I have a query in rethinkdb but show a problem when i try remove items that have one attrib repeat.
data table
[
{
codeQR: '100001597182620700',
numid: '1000081',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
},
{
codeQR: '100001597182749578',
numid: '1000082',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
},
{
codeQR: '100001597185279006',
numid: '1000082',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
},
{
codeQR: '100001597183951080',
numid: '1000082',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
},
{
codeQR: '100001597183951216',
numid: '1000083',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
},
{
codeQR: '100001597185279182',
numid: '1000083',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
},
{
codeQR: '100001597182864764',
numid: '1000083',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
},
{
codeQR: '100001597185307862',
numid: '1000084',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
},
{
codeQR: '100001597183974288',
numid: '1000084',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
},
{
codeQR: '100001597183002590',
numid: '1000084',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
}
]
query with rethinkdb
r.db('myDb').table('userSearchData')
.filter(querys=>
querys('numid').gt('1000080')
.and(
querys('numid').lt(String('1000085'))
)
)
.limit(5)
and this query show it:
[
{
codeQR: '100001597182620700',
numid: '1000081',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
},
{
codeQR: '100001597182749578',
numid: '1000082',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
},
{
codeQR: '100001597185279006',
numid: '1000082',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
},
{
codeQR: '100001597183951080',
numid: '1000082',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
},
{
codeQR: '100001597183951216',
numid: '1000083',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
}
]
so, the problem is because if you see, the array has a items with attrib numid and i want that only it shows one item of same numid. I resolved it with reduce() javascript function like that
let datas=resultQuery.reduce((arry, val)=>{
if(arry.length){
if(!arry.some(val2=>val2.numid===val.numid)){
arry.push(val)
}
}else
arry.push(val)
return arry
}, [])
console.log(datas, 'FT array filter', __filename)
and final result is:
[
{
codeQR: '100001597182620700',
numid: '1000081',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
},
{
codeQR: '100001597182749578',
numid: '1000082',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
},
{
codeQR: '100001597183951216',
numid: '1000083',
user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
}
]
But only has 3 item of 5 that i ordered
Img shows than me want with that rethinkdb

You can use group to group your data by matching numid. Then select the first entries in those group. I am not sure what the desired selection should be, but you can implement any criteria in the map function.
r.db('myDb').table('userSearchData')
.filter(querys=>
querys('numid').gt('1000080')
.and(
querys('numid').lt(String('1000085'))
)
)
.group("numid")
.ungroup()
.map(r.row("reduction")(0))
.limit(5)
Grouping the data will result in buckets as follows:
[
{
"group": "1000081",
"reduction": [
{
"codeQR": "100001597182620700",
"numid": "1000081",
"user": "a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5"
}
]
},
{
"group": "1000082",
"reduction": [
{
"codeQR": "100001597183951080",
"numid": "1000082",
"user": "a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5"
},
{
"codeQR": "100001597182749578",
"numid": "1000082",
"user": "a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5"
},
{
"codeQR": "100001597185279006",
"numid": "1000082",
"user": "a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5"
}
]
}
//....
]
Then you will have to use the ungroup function to continue adding methods to the query. And with map() you select the first element of each reduction.
then your result is:
[
{
"codeQR": "100001597182620700",
"numid": "1000081",
"user": "a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5"
},
{
"codeQR": "100001597183951080",
"numid": "1000082",
"user": "a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5"
},
{
"codeQR": "100001597185279182",
"numid": "1000083",
"user": "a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5"
},
{
"codeQR": "100001597183974288",
"numid": "1000084",
"user": "a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5"
}
]

The result that you are getting is correct. I dont know exactly what your expectations are:
let datas=resultQuery.reduce((arry, val)=>{
if(arry.length){
if(!arry.some(val2=>val2.numid===val.numid)){
arry.push(val)
}
}else
arry.push(val)
return arry
}, [])
console.log(datas, 'FT array filter', __filename)
at the first item arry.length is false therefore you just add the item
second item you check if numid is not yet existing -> add item
numid already exist -> skip
numid already exist -> skip
numid new -> add item

Related

Kendo UI Chart - Visualize count of returned JSON fields

I want to display the counts of specific retrieved fields in my pie/donut chart.
I'm retrieving data via REST and the result is in json format. The source is a list repeating values:
Example: In the following list, I'd like to get a present the number (count) of completed responses; perhaps in a second chart present the breakdown of responses by location.
var userResponse = [
{ User: "Bob Smith", Status: "Completed", Location: "USA" },
{ User: "Jim Smith", Status: "In-Progress", Location: "USA" },
{ User: "Jane Smith", Status: "Completed", Location: "USA" },
{ User: "Bill Smith", Status: "Completed", Location: "Japan" },
{ User: "Kate Smith", Status: "In-Progress", Location: "Japan" },
{ User: "Sam Smith", Status: "In-Progress", Location: "USA" },
]
My Initialization currently looks like this:
$('#targetChart').kendoChart({
dataSource: {
data: data.d.results,
group: {
field: "Location",
},
},
seriesDefaults: {
type: "donut",
},
series: [{
field: 'Id',
categoryField: 'Location',
}],
});
You can easily transform the data. Read it into a DataSource object grouping by location and filtering for completed only. Then fetch the data and create an array of the counts for each location:
var pieData = [];
var respDS = new kendo.data.DataSource({
data: userResponse,
group: {
field: "Location",
},
filter: {
field: "Status",
operator: "eq",
value: "Completed" },
});
respDS.fetch(function(){
var view = respDS.view();
for (var i=0; i<view.length; i++){
var item = {};
item.Location = view[i].value;
item.Count = view[i].items.length;
pieData.push(item);
}
});
You end up with:
[
{Location: "Japan", Count: 1},
{Location: "USA", Count: 2},
]
This can then be bound to a pie/donut.
DEMO

How to write Query to retrieve every subdocument array who matches

Here i need my explain my problem clearly. How can i write a query to retrieve every subdocument array who matches the condition using mongoose and nodejs.
this is my existing JSON:
[
{
_id: "568ccd6e646489f4106470ec",
area_name: "Padi",
warehouse_name: "Hapserve Online Water Service",
name: "Ganesh",
email: "ganesh#excrin.com",
mobile_no: "9042391491",
otp: "4466",
__v: 0,
date: "06-01-2016",
booking:
[
{
can_quantity: "4",
delivery_date: "06-01-2016",
delivery_timeslot: "10am-3pm",
subscription: "true",
subscription_type: "Weekly",
total_cost: "240",
order_id: "S13833",
can_name: "Tata Waters",
address: "15/A,Ramanrajan street,,Padi,Chennai",
can_cost: "240",
_id: "568ccd6e646489f4106470ee",
ordered_at: "2016-01-06T08:16:46.825Z",
status: "UnderProcess"
},
{
can_name: "Bisleri",
can_quantity: "4",
can_cost: "200",
delivery_date: "11-01-2016",
delivery_timeslot: "3pm-8pm",
order_id: "11537",
address: "27,Main Street,Padi,Chennai",
_id: "5693860edb988e241102d196",
ordered_at: "2016-01-11T10:38:06.749Z",
status: "UnderProcess"
}
]
},
{
_id: "56937fb8920629a0164604d8",
area_name: "Poonamallee",
warehouse_name: "Tata Waters",
name: "M.Kalaiselvan",
email: "kalai131192#gmail.com",
mobile_no: "9003321521",
otp: "2256",
__v: 0,
date: "2016-01-11T10:11:04.266Z",
booking:
[
{
can_quantity: "4",
delivery_date: "06-01-2016",
delivery_timeslot: "10am-3pm",
subscription: "true",
subscription_type: "Alternate",
total_cost: "640",
order_id: "S13406",
can_name: "Kinley",
address: "133,Bajanai koil street, Melmanagar,Poonamallee,Chennai",
can_cost: "160",
_id: "56937fb8920629a0164604da",
ordered_at: "11-01-2016",
status: "UnderProcess"
},
{
can_name: "Tata Waters",
can_quantity: "2",
can_cost: "120",
delivery_date: "11-01-2016",
delivery_timeslot: "10am-3pm",
order_id: "11387",
address: "140,Bajanai koil street, Melmanagar,Poonamallee,Chennai",
_id: "56937ff7920629a0164604dc",
ordered_at: "2016-01-11T10:12:07.719Z",
status: "UnderProcess"
},
{
can_name: "Bisleri",
can_quantity: "4",
can_cost: "200",
delivery_date: "12-01-2016",
delivery_timeslot: "10am-3pm",
order_id: "16853",
address: "140,Bajanai koil street, Melmanagar,Poonamallee,Chennai",
_id: "56938584db988e241102d194",
ordered_at: "2016-01-11T10:35:48.911Z",
status: "UnderProcess"
},
{
can_name: "Hapserve",
can_quantity: "6",
can_cost: "150",
delivery_date: "11-01-2016",
delivery_timeslot: "10am-3pm",
order_id: "17397",
address: "133,Bajanai koil street, Melmanagar,Poonamallee,Chennai",
_id: "569385bbdb988e241102d195",
ordered_at: "2016-01-11T10:36:43.918Z",
status: "UnderProcess"
},
{
can_name: "Bisleri",
can_quantity: "5",
can_cost: "250",
delivery_date: "11-01-2016",
delivery_timeslot: "10am-3pm",
order_id: "14218",
address: "133,Bajanai koil street, Melmanagar,Poonamallee,Chennai",
_id: "56939a13c898ef7c0cc882b0",
ordered_at: "2016-01-11T12:03:31.324Z",
status: "Cancelled"
}
]
}
]
Here i need to retrieve every document where delivery date is today
so this is my nodejs route
router.get('/booking-date/:date', function(req, res){
var id = req.params.date;
RegisterList.find({'booking.delivery_date':id}, {'booking.$':1}, function(err, docs){
if(err)
throw err;
res.json(docs);
});
});
while am using this am not able to get every data. only two data is retrieve from collection.
example if i search for a date 11-01-2016 am getting only one subdocument for
each parent id, but in the above json for date 11-01-2016. for one parent id has 2 subdocument for that date and another parent id has 1 subdocument for that date.
am not able to write mongoose query retrieve to every subdocument where matches done..
Help will be appreciated...
Sounds like you may want to try the aggregation framework where you can $project the booking array with a filter made possible using the $setDifference, $map and $cond operators.
The $map operator inspects each element within the booking array and the $cond operator returns only the wanted fields based on a true condtion, a false value is returned on the contrary instead of the array element. $setDifference operator then removes all false values from the array by comparing to another set with the [false] values, the final result is only the returned matches:
router.get('/booking-date/:date', function(req, res){
var id = req.params.date,
pipeline = [
{
"$match": { 'booking.delivery_date': id }
},
{
"$project": {
"booking": {
"$setDifference": [
{
"$map": {
"input": "$booking",
"as": "el",
"in": {
"$cond": [
{ "$eq": [ "$$el.delivery_date", id ] },
"$$el",
false
]
}
}
},
[false]
]
}
}
}
];
RegisterList.aggregate(pipeline, function(err, docs){
if(err) throw err;
res.json(docs);
});
});
The $ projection operator projects only first matching element, refer here
Project all subdocuments {bookings: 1},then filter subdocuments within your application.

How to perform join in sails JS

I have documents of the form
challenge:
{
"name": "challenge by abdul",
"created_user_id": "1",
"game_id": "123",
"platform_id": "9857",
"amount": 30
}
game:
{
"_id": "auto_generated",
"name": "NFS",
"version": "3",
}
platform:
{
"_id": "auto_generated",
"name": "ps2",
"version": "sami"
}
I want to perform join query in sails and want result in below format
{
"name": "challenge by abdul",
"created_user_id": "1",
"game_id": "123",
"game_name":"NFS",
"platform_name": "ps2",
"platform_id": "9857",
"amount": 30
}
There is no join in Sails but populate. So you need make associations between models and populate them. Example:
// api/models/Platform.js
module.exports = {
attributes: {
name: {
type: 'string'
},
version: {
type: 'string'
},
game: {
model: 'Game',
via: 'platform'
}
}
};
// api/models/Game.js
module.exports = {
attributes: {
name: {
type: 'string'
},
version: {
type: 'string'
},
platform: {
model: 'Platform',
via: 'game'
}
}
};
You can write following code then:
// api/controllers/AnyController.js
module.exports = {
index: function(req, res) {
Game
.findOne({name: 'MY_GAME'})
.populate('platform')
.then(function(game) {
console.log(game); // Game model
console.log(game.platform); // Platform model
return game;
})
.then(res.ok)
.catch(res.negotiate);
}
};

Elasticsearch: how to store all json objects dynamically

I'm trying to store all Json objects through elasticsearch.
client.create({
index: 'index',
type: 'type',
id:"1"
body:result[0]
},function (error,response)
{
if (error)
{
console.log('elasticsearch cluster is down!');
}
else
{
console.log('All is well');
}
});
In this result[0] I'm getting my first value of a Json object but I need to store all Json objects dynamically.
The output which i'm getting is:
-> POST http://localhost:9200/index/type/1?op_type=create
{
"Name": "Martin",
"Age": "43",
"Address": "trichy"
}
<- 201
{
"_index": "index",
"_type": "type",
"_id": "1",
"_version": 4,
"created": true
}
But I need an output like this:
-> POST http://localhost:9200/index/type/1?op_type=create
{
"Name": "Martin",
"Age": "43",
"Address": "trichy"
},
{
"Name": "vel",
"Age": "23",
"Address": "chennai"
},
{
"Name": "ajay",
"Age": "23",
"Address": "chennai"
}
<- 201
{
"_index": "index",
"_type": "type",
"_id": "1",
"_version": 4,
"created": true
}
What you need is to use the bulk endpoint in order to send many documents at the same time.
The body contains two rows per document, the first row contains the index, type and id of the document and the document itself is in the next row. Rinse and repeat for each document.
client.bulk({
body: [
// action description
{ index: { _index: 'index', _type: 'type', _id: 1 } },
// the document to index
{ Name: 'Martin', Age: 43, Address: 'trichy' },
{ index: { _index: 'index', _type: 'type', _id: 2 } },
{ Name: 'vel', Age: 23, Address: 'chennai' },
{ index: { _index: 'index', _type: 'type', _id: 3 } },
{ Name: 'ajay', Age: 23, Address: 'chennai' }
]
}, function (err, resp) {
// ...
});
I suspect your result array is the JSON you get from your other question from yesterday. If so, then you can build the bulk body dynamically, like this:
var body = [];
result.forEach(function(row, id) {
body.push({ index: { _index: 'index', _type: 'type', _id: (id+1) } });
body.push(row);
});
Then you can use the body in your bulk call like this:
client.bulk({
body: body
}, function (err, resp) {
// ...
});

jsTree populated with JSON string

i got this jsTree:
$(function () {
$("#tree").jstree({
"json_data" : {
"data" : [
{
"data" : "<?php echo $db_obj->getValue('article_group_name') ?>",
"metadata" : { id : 23 },
"children" : [ "Child 1", "A Child 2" ]
}
]
},
"plugins" : ["themes","json_data", "ui" ]
});
});
I would like to populate it with DB data. The Childs should be line from the database.
I json_encoded the table data, it looks something like this:
[Object { article_id=
"4949"
, article_name_internal=
"Nachtlampe Lumilove Barbapapa"
}, Object { article_id=
"4947"
, article_name_internal=
"Wanduhr Silk von Koziol"
},
Whene i click one of the childs it should go to that page. Not sure how i can populate the tree with this data. Any instructions?
Each node for jsTree have a list of attributes that you can set to it.
just use the attr property in your JSON and add an array of property-value pairs that represent the data you want.
one of these properties should be an href containing the URL for the page you want to opent once someone clicks the node for your jsTree.
now your server should return the data like this.
{
"data": "Root",
"attr": {
"id": "1",
"rel": "Root",
"type": 0
},
"children": [{
"data": "Test 1",
"attr": {
"id": "2",
"href": "http://www.google.com"
"rel": "OrganizationalUnit",
"type": 1
},
"children": [],
"state": "open"
}],
"state": "open"
}
and your JSTree inint function should do something like that:
k_OrgTree = $("#OrgTree").jstree({
json_data: {
ajax: {
url: "/Administration/PopulateTree",
type: "POST",
dataType: "json",
contentType: "application/json charset=utf-8",
success: function (data) { }
}
},
themes: currentTheme,
types: {
valid_children: [k_Root],
types: {
Root: {
valid_children: [k_OrganizationalUnit, k_Calendar],
icon: { image: "/Content/Administration/Images/Root/Root_32x32.png" },
delete_node: false,
},
OrganizationalUnit: {
valid_children: [k_OrganizationalUnit, k_Calendar, k_User],
icon: { image: "/Content/Administration/Images/OrganizationalUnit/OrganizationalUnit_32x32.png" },
},
Calendar: {
valid_children: ["none"],
icon: { image: "/Content/Administration/Images/Calendar/Calendar_32x32.png" },
create_node: false
},
User: {
valid_children: ["none"],
icon: { image: "/Content/Administration/Images/User/User_32x32.png" },
create_node: false
}
}
},
plugins: ["themes", "json_data", "types", "ui"]
});