Can't fetch data from table I've just created in rethinkDB.
I've create a new table in retinkDB - items.
And fill it with data:
r.db('test').table('items').insert([
{name: 'qqq'},
{name: 'www'},
{name: 'eee'}
])
BUT .getList() never returns table's data:
client.record.getList('items') // '.getEntries()' always return []
I don't really understand why .getList('items') didn't return data from items table.
I assume that this is because entries structure: when you are create entry via deepstream, entry's structure be like:
[
...
{
"_d": {
"id": "qqq"
"name": 'qqq'
},
"_v": 0,
"ds_id": "qqq"
}
...
]
But mine structure is just:
[
{
"id": 'qqq'
"name": 'qqq'
}
]
My question is: How to create table with data in rethinkDB (via script) that would we worked with deepstream?
deepstream is designed to manage data for you. Simply by saying
var bmw = ds.record.getRecord( 'cars/bmw' )
deepstream will create a table called cars and store an entry with a primary key of bmw in it. You wouldn't be expected to create the table yourself.
I've find a solition.
First of all table should be created with proper primaryKey === "ds_id":
r.db('test')
.tableCreate('items',
{
primaryKey: 'ds_id'
})
And after that you have to insert data with deepstream-like structure:
r.db('test').table('items').insert([
{
"_d": {
"id": r.uuid(),
"name": "qqq"
},
"_v": 0,
"ds_id": r.uuid()
}
])
P.S. to make _d.id equal to ds_id use this:
r.db('test').table('items').forEach(function(c) {
return r.db('test').table('items').get(c('ds_id')).update(function(row) {
return {_d: {id: row('ds_id')}};
});
})
This is stupid but I don't know how to do it in more elegant way.
Related
I want to update progress of the particular course by finding userId and coursename. I have seen all the MongoDB queries for that but still not getting the desired output. Sometimes got an empty array while data exist in DB. I am not getting if I have to add value inside the progress array then how I will apply the MongoDB query on that:
{
userId: "218u092ue029ie",
ABC:{
"courseName": "course1",
"progress": [
1,2,3
]
},
XYZ:{
"courseName": "course2",
"progress": [
1,2
]
},
pqr:{
"courseName": "course3",
"progress": [
1,2,3,4,5
]
}
}
Try like this:
db.yourCollection.updateOne(
{ userId: "218u092ue029ie", "ABC.courseName": "course1" },
{ $set: { "ABC.progress.$": [1, 2, 3, 4] } }
);
I need to get filter data but it works when its only list of data not list in list.
There is some lists of object name starting with number1 with list but need the data inside number and save it in array. Using smart table vuejs so if i get json data it will work since i did similar things.
This is my code
let result = [];
let response = await this.$axios.post("api");
for (item in response.data.list){
result.push(response.data.list[item][0];
}
{
"list": {
"number1": [
{},
],
"number2": [
{}
],
"number3": [
{}
],
etc...
}
<v-table
//it is within thead and
:data="result"
:filters= "filters">
<input v-model="filters.name.value"
<v-table/>
data:function(){
return {
result: [];
filters: {
... //other fields same as below
name: { value: "", keys: ["name"] },
...
},
}
}
I have this schema:
...
recommended:{
type:[{
movieId:String,
recommendedBy:[String]
}],
default:[],
},
name: {
type: String,
}
from the request I get movieId and id.
I want to add to recommended array a new object if there is no other item with the same movieId there already, and add theid to the recommendedBy nested array in both cases.
so far I have this:
const user = User.findByIdAndUpdate({_id:'123'},
{$AddToSet:{'recommended.$[movie].recommendedBy':id}},
{arrayFilters:[{'movie.movieId':movieId}]})
this will only set the recommendedBy of an already existing item with the same movieId,
but will not push a new object with the new movieId property.
(so it's basically $AddToSet for both arrays)
How could I achieve that? thanks!
Maybe you need something simple like this that is doing upsert or update for the recommended array element:
db.collection.update({},
[
{
$set: {
recommended: {
"$reduce": {
"input": "$recommended",
"initialValue": // assign your edit data as init value
[
{
"movieId": "1",
"recommendedBy": "who recommended"
}
],
"in": {
"$cond": {
"if": {
$in: [
"$$this.movieId",
"$$value.movieId"
]
},
"then": "$$value",
"else": {
"$concatArrays": [
"$$value",
[
"$$this"
]
]
}
}
}
}
}
}
}
])
explained:
No filter but you can add anything in the initial document filter to search only limited set of documents.
In the update stage you use $reduce to check input array elements that you need to upsert or update the value in the affected array element.
( Confirmed to work in mongod 4.4 )
playground
I couldn't understand #R2D2's answer so I came up with this workaround which does unexpectedly well for me, since I also need to be able to remove the movieId and all the recommendedBy with it.
I changed my schema to this:
recommanded:{
type: Map,
of:[String],
default:{}
},
I'm removing/ adding with:
User.findByIdAndUpdate({_id:"123",{$addToSet:{[`recommended.${movieId}`]:id}})
And
User.findByIdAndUpdate({_id:"123",{$unset:{[`recommended.${movieId}`]:""}})
I want to use this data (below) as a data source for a d3.js program:
{
"component": {
"name1": {
"graphname": {
"title": "foo",
"data": [
{"data": "DATE IN ISOFORMAT", "value": 5},
{"data": "DATE IN ISOFORMAT", "value": 10}
]
}
},
"name2": {
"graphname": {
"title": "foo",
"data": [
{"data": "DATE IN ISOFORMAT", "value": 5},
{"data": "DATE IN ISOFORMAT", "value": 10}
]
}
}
}
"component2": {...
}
D3 accepts only array data? How i can manipulate it to work?
I want to graph for all component any "graphname" aggregated by "name"
Any tips?
D3 does not just accept arrays as data sources, but for looping purposes, arrays are much more convenient than JavaScript objects ("dicts"). There is a very easy way to convert objects to arrays, though. If your object above were called d, then its corresponding array can be created with:
var dlist = d3.entries(d);
Now dlist will be something like:
[ { key: 'component',
value: { name1: ..., name2: ... } },
{ key: 'component2',
value: { name1: ..., name2: ... } } ]
The original dict has been remapped into an array of "records," each with key and value pairs. This "array of records" pattern is very common in D3 work, and JavaScript in general. If you need to loop over the sub-structures (e.g. the name1, name2, ... values, d3.entries can be applied at multiple levels of the original structure, as those dict-to-list transforms are required.
Since this answer is called out in the comments as wrong, here's a working example of using an object ("dict") as a data source for a D3 program: first in a simple loop, then secondarily using the idiomatic d3 .data(...).enter() pipeline.
I have an JSON array like this
var filter_value_data = [{"Status":[{"name":"Open","id":"1"},{"name":"Pending","id":"2"},{"name":"Resolved","id":"3"},{"name":"Closed","id":"4"},{"name":"Evaluation","id":"5"}]},{"Payment Status":[{"name":"Paid","id":"10"},{"name":"UnPaid","id":"11"},{"name":"Part Paid","id":"12"}]},{"Priority":[{"name":"Low","id":"6"},{"name":"Medium","id":"7"},{"name":"High","id":"8"},{"name":"Urgent","id":"9"}]}]
I have tried filter_value_data["Status"] which is obviously wrong. How do I get the JSON elements for Status using the names like Status,Payment Status?
filter_value_data is an array (having []), so use filter_value_data[0].Status to get the first element-object with property "Status".
It is always good to format your code in order to see the hierarchy of the structures:
var filter_value_data = [
{
"Status": [
{
"name": "Open",
"id": "1"
}, {
"name": "Pending",
"id": "2"
}, ...
]
}, {
"Payment Status": [
{
"name": "Paid",
"id": "10"
}, ...
]
}, {
"Priority": [
{
"name": "Low",
"id": "6"
}, ...
]
}
];
With your current JSON you can't get the elements with the name alone.
You can get Status with filter_value_data[0]['Status'] and Payment status with filter_value_data[1]['Payment Status'].
This is because the keys are in seperate objects in the array.
In order to get them with filter_value_data['Status'] you need to change your JSON to
var filter_value_data = {
"Status":[
{"name":"Open","id":"1"},
{"name":"Pending","id":"2"},
{"name":"Resolved","id":"3"},
{"name":"Closed","id":"4"},
{"name":"Evaluation","id":"5"}
],
"Payment Status":[
{"name":"Paid","id":"10"},
{"name":"UnPaid","id":"11"},
{"name":"Part Paid","id":"12"}
],
"Priority":[
{"name":"Low","id":"6"},
{"name":"Medium","id":"7"},
{"name":"High","id":"8"},
{"name":"Urgent","id":"9"}
]
};
I wrote this on my phone so it's not as well-formatted as usual. I'll change it ASAP.
With your current JSON, created a result which might be helpful for you.
JS:
$.each(filter_value_data,function(ind,val){
var sta = val.Status; // Status Object get displayed
for(var i=0;i<sta.length;i++){
var idVal= sta[i].id;
var nameVal = sta[i].name;
Statusarray.push(idVal,nameVal);
console.log(Statusarray);
}
})
FiddleDemo
You can use below code, it will return status object
filter_value_data[0]['Status']
filter_value_data[0]['Payment Status']
to get Single value you use :
filter_value_data[0]['Status'][0]['name']