Using the example given on the Sails.js documentation page, I get the following error : "Maximum call stack size exceeded error".
items.find().populate("user", {name:givenName}).exec(function(err, response) {
console.log(response);
}
The matter is that when I look with wireshark, the query on the database tries to find every user (and the database is big), but it doesn't make any join.
items
+-----------+--------------+------+-----+
| Field | Type | Null | Key |
+-----------+--------------+------+-----+
| id | int(6) | NO | PRI |
| name | varchar(255) | NO | |
+-----------+--------------+------+-----+
user
+-----------+--------------+------+-----+
| Field | Type | Null | Key |
+-----------+--------------+------+-----+
| id | int(6) | NO | PRI |
| name | varchar(255) | NO | |
| item_id | int(11) | YES | MUL |
+-----------+--------------+------+-----+
Should be as simple as:
items
.find()
.populate("user")
.where({name: givenName}) //or .where({user: {name: givenName}})
.exec(function(err, response) {
console.log(response);
});
Ok, If like you said you want only items for a specific user do this :
user.findOneByName(givenName).exec(function(err, user) {
items.find().where({user : user.id}).exec(function(err, response) {
console.log(response);
});
});
Related
I just doing project import excel using laravel.
But there is an error when import / upload file excel and for web page can open and work.
ErrorException
Undefined array key 7
public function model(array $row)
{
return new Dso([
'id_dso' => $row[1],
'id_rso' => $row[2],
'id_focus' => $row[3],
'id_wilayah' => $row[4],
'id_grup_wilayah' => $row[5],
'nama_dso' => $row[6],
'status' => $row[7],
]);
}
and my table on database format is
+-----------------+---------------------+------+-----+---------------------+-------------------------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------------------+-------------------------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| id_dso | bigint(20) unsigned | NO | | NULL | |
| id_rso | bigint(20) unsigned | NO | | NULL | |
| id_focus | bigint(20) unsigned | NO | | NULL | |
| id_wilayah | bigint(20) unsigned | NO | | NULL | |
| id_grup_wilayah | bigint(20) unsigned | NO | | NULL | |
| nama_dso | varchar(255) | NO | | NULL | |
| created_by | varchar(255) | NO | | NULL | |
| created_date | timestamp | NO | | current_timestamp() | on update current_timestamp() |
| modified_by | varchar(255) | NO | | NULL | |
| modified_date | timestamp | YES | | NULL | |
| status | tinyint(1) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+-----------------+---------------------+------+-----+---------------------+-------------------------------+
14 rows in set (0.009 sec)
and my data sample is
Import data Sample
I'm using laravel 8.6 and my datbase is MariaDb.
Please start your array $row index from $row[0] instead of $row[1] as given as under
public function model(array $row)
{
return new Dso([
'id_dso' => $row[0],
'id_rso' => $row[1],
'id_focus' => $row[2],
'id_wilayah' => $row[3],
'id_grup_wilayah' => $row[4],
'nama_dso' => $row[5],
'status' => $row[6],
]);
}
Try this.
Array starts with index 0.
So start from $row[0] until $row[6]
The key status will be $row[6] in your case.
public function model(array $row)
{
return new Dso([
'id_dso' => $row[0],
'id_rso' => $row[1],
'id_focus' => $row[2],
'id_wilayah' => $row[3],
'id_grup_wilayah' => $row[4],
'nama_dso' => $row[5],
'status' => $row[6],
]);
}
try this
public function model(array $row)
{
return new Dso([
'id_dso' => $row[1],
'id_rso' => $row[2],
'id_focus' => $row[3],
'id_wilayah' => $row[4],
'id_grup_wilayah' => $row[5],
'nama_dso' => $row[6],
'status' => ($row[7]) ? $row[7] : ' ',
]);
}
I made some api system with nodejs.
Also I use sequelize(version 4) for communicates with MySQL.
Below is my model structure.
[user]
no(PK)
userid
userpw
[article]
no(PK)
subject
content
writer(FK to user's userid)
I made with sequelize orm define like this.
import Sequelize from 'sequelize';
const sequelize = new Sequelize('db', 'user', 'pw', {
host: '127.0.0.1',
dialect: 'mysql'
})
const User = sequelize.define('user', {
no: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
userid: {
type: Sequelize.STRING(30),
allowNull: false,
unique: true
},
userpw: {
type: Sequelize.STRING(30),
allowNull: false
}
}, {
freezeTableName: true,
timestamps: true,
underscored: true
});
const Article = sequelize.define('article', {
no: {
type: Sequelize.INTEGER,
primaryKey: true
},
subject: {
type: Sequelize.STRING(50),
allowNull: false
},
content: {
type: Sequelize.STRING(100),
allowNull: false
},
writer: {
type: Sequelize.STRING(30),
references: {
model: 'user',
key: 'userid'
}
}
}, {
freezeTableName: true,
timestamps: true,
underscored: true
})
sequelize.sync({
force: true
});
export default { User, Article };
In Article model, I define write and refer to User's userid.
So Article's writer column is Foreign Key refer to User's userid.
Because If user was deleted, related article have to delete.
Run that code, table will be created and structure is like this.
mysql> desc user;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| no | int(11) | NO | PRI | NULL | auto_increment |
| userid | varchar(30) | NO | UNI | NULL | |
| userpw | varchar(30) | NO | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> desc article;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| no | int(11) | NO | PRI | NULL | |
| subject | varchar(50) | NO | | NULL | |
| content | varchar(100) | NO | | NULL | |
| writer | varchar(30) | YES | MUL | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
After do that, I searched more information about association in sequelize orm.
And I found that sequelize has many association like hasMany, BelongsTo, 'hasMany`, ...
So I add User.hasMany(Article); to my previous code and run it, it add more columns.
mysql> desc article;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| no | int(11) | NO | PRI | NULL | |
| subject | varchar(50) | NO | | NULL | |
| content | varchar(100) | NO | | NULL | |
| writer | varchar(30) | YES | MUL | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| user_no | int(11) | YES | MUL | NULL | |
+------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
user_no column was added.
I think it automatically added by hasMany function.
But in my case, I do not have to create user_no, Because I already refer to User's user_id column.
Overall, my question is:
I think I do not have to set hasMany in my case. Is it right?
Why ORM offer assocation function? Just for convenience? or Is there any reason for it?
Thanks.
I have an attribute with null value. and some rows have boolean value. Now I want to write a filter expression to filter for the rows with attribute having null value. I am using dynamodb document client.
If you look at the docs, for a scan you can do:
ScanFilter: {
'<AttributeName>': {
ComparisonOperator: EQ | NE | IN | LE | LT | GE | GT | BETWEEN | NOT_NULL | NULL | CONTAINS | NOT_CONTAINS | BEGINS_WITH, /* required */
AttributeValueList: [
someValue /* "str" | 10 | true | false | null | [1, "a"] | {a: "b"} */,
/* more items */
]
},
/* '<AttributeName>': ... */
}
If you are using query:
QueryFilter: {
'<AttributeName>': {
ComparisonOperator: EQ | NE | IN | LE | LT | GE | GT | BETWEEN | NOT_NULL | NULL | CONTAINS | NOT_CONTAINS | BEGINS_WITH, /* required */
AttributeValueList: [
someValue /* "str" | 10 | true | false | null | [1, "a"] | {a: "b"} */,
/* more items */
]
},
/* '<AttributeName>': ... */
},
They both clearly support filtering by attributes with value null.
My search query is:
query: {
match: {
name: "le sul"
}
},
I expect to see the output as:
.-------------------------------------------------.
| ID | Name | Score |
|-------|-----------------------------|-----------|
| 9 | le sultan | ... |
| 467 | le sultan | ... |
| 23742 | LE DUONG | 1.1602057 |
| 11767 | LE VICTORIA | 0.9554229 |
| 11758 | LE CANONNIER | 0.9554229 |
| 23762 | PHA LE XANH | 0.9281646 |
| 15795 | LE SURCOUF HOTEL & SPA | 0.9281646 |
| 33066 | LE CORAL HIDEAWAY BEYOND | 0.8695703 |
| 11761 | LE MERIDIEN MAURITIUS | 0.8682439 |
| 11871 | LE RELAX HOTEL & RESTAURANT | 0.8682439 |
'-------------------------------------------------'
But what I see is:
.-------------------------------------------------.
| ID | Name | Score |
|-------|-----------------------------|-----------|
| 23742 | LE DUONG | 1.1602057 |
| 9 | le sultan | 1.0869629 | <----
| 11767 | LE VICTORIA | 0.9554229 |
| 11758 | LE CANONNIER | 0.9554229 |
| 467 | le sultan | 0.9554229 | <----
| 23762 | PHA LE XANH | 0.9281646 |
| 15795 | LE SURCOUF HOTEL & SPA | 0.9281646 |
| 33066 | LE CORAL HIDEAWAY BEYOND | 0.8695703 |
| 11761 | LE MERIDIEN MAURITIUS | 0.8682439 |
| 11871 | LE RELAX HOTEL & RESTAURANT | 0.8682439 |
'-------------------------------------------------'
As you can see, "le sultan" not the first element of the result set.
Where am I going wrong?
Your query result is not match because elasticsearch search via _score.
In your case you want to search in an analyzed search and get result in not analyzed.
So you should put your mapping like given below
Put your_index_name
{
"mappings": {
"your_type_name": {
"properties": {
"name": {
"type": "string",
"analyzer": "english",
"fields": {
"your_temporary_sort_filed_name": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
And then
GET /your_index_name/your_type_name/_search
{
"sort": [
{
"name.your_temporary_sort_filed_name":{
"order": "desc"
}
}
],
"query": {
"match": {
"name": "le sul"
}
}
}
If you want to get le sultan as use following query:
{
"query": {
"query_string": {
"default_field": "name",
"query": "le sul*"
}
}
}
Good day all, can someone suggest me which async function should i use?,
in my case, I need an output the 1st member of each group.
*** note this is just a example of the flow of my program.
/*
Grouplist groupmember
Id | name id | name | group
1 | group1 1 | John | 1
2 | group2 2 | James | 1
3 | group3 3 | Paul | 3
4 | Angel | 2
5 | Anne | 2
6 | Jane | 3
looking for output like this
id | name | group
1 | John | 1
4 | Angel | 2
3 | Paul | 3
*/
var name = [];
Grouplist.find( function(err, grouplist){
for(var x=0;x<=grouplist.length-1; x++){
groupmember.find({id:grouplist[x].id}).limit(1).exec(
function callBack(err,results){
if(err) console.log(err);
name[x] = results.name;
})
}
})
You can get the result in one query using mongodb aggregate operators:
groupmember.aggregate(
[
{
$group:
{
_id: "$group", //grouping key
member: { $first: "$$CURRENT" } //return the first matched document
}
},
{
$project : //project the document to top-level
{
_id: "$member._id",
name: "$member.name",
group: "$member.group"
}
}
],
function(err, members){
console.log(members)
}
)