I want to run more than one query .. how to do it?
eg, I have below two queries -
FOR doc IN users
RETURN doc
FOR doc IN users
RETURN { user: doc, newAttribute: true }
If I have to run both queries I have to run them separately, is there a way to execute a script or I need to put a semicolon at the end like SQL and run it.
Can I use arangosh?
You can use LET to execute multiple sub-queries in a single queries:
LET firstUserResult = (
FOR doc IN users
RETURN doc
)
LET secondUserResult = (
FOR doc IN users
RETURN { user: doc, newAttribute: true }
)
RETURN { first: firstUserResult, second: secondUserResult }
Some notes here - you will need to add an additional RETURN statement at the end of the query. This will definitely work for reads but you may run into issues when trying to write in multiple queries.
Related
So I was going through a project codebase(nodejs 10) and they are using sequelize for executing queries in Mysql and at some places they are using useMaster: true.
I am just wondering what is it there for? Anyone having any idea?
Example code:
[error , coupons ] = await to(#wagner.get('sequelize').query( query , { useMaster: true, replacements : [coupon_offset,parseInt(limit)] ,type: #wagner.get('sequelize').QueryTypes.SELECT } ))
Didi you check the official documentation?
It says about useMaster in the Params section of query instance method:
Force the query to use the write pool, regardless of the query type.
See query method
I'm not sure if it's applicable to MySQL but still I'd recommend to find out more about write pools.
Is there a way to get Sequelize to build a query with the replacements (so I'll be able to use their SQL injection cleanup) and just get the raw SQL query, without executing it?
You can just call the QueryGenerator with the type of query you want to generate.
For example a selectQuery:
const sql = MyModel.QueryGenerator.selectQuery(
MyModel.getTableName(), {
where: {
someAttribute: "value"
},
attributes: ["other", "attributes"]
},
MyModel);
There's also insertQuery, updateQuery, deleteQuery too.
It looks like this feature isn't implemented yet, but there are some users trying to push the issue forward.
See github issue.
it's undocumented, but I use next way (Sequelize ver. 5.2.13):
let tableName = myModel.getTableName(options);
let qi = myModel.QueryInterface;
options.type = 'SELECT';
options.model = myModel;
var sql = qi.QueryGenerator.selectQuery(tableName, options, myModel);
I have a query in neo4j with some aggregation functions, which takes around 10 seconds to retrieve the information. What I would like to do is store the query results into redis and from time to time the redis database would update the results from neo4j.
One record will be like:
{ entry: "123", model: "abc", reactants: [{specie: "abc#12", color: "black"}], .... }
I'm using node.js with express, thank you in advance for your attention
UPDATE: my query is quite extensive, I had to do that 'UNWIND' part to be able to search by the reactants (I wanted the products too but I didn't know how to do it). I don't know if it's possible to be optimized to at least 2 seconds but here it goes:
MATCH (rx:ModelReaction),
(rx)-[l:left_component]->(lc:MetaboliteSpecie),
(rx)-[r:right_component]->(rc:MetaboliteSpecie)
OPTIONAL MATCH (rx)-[:has_gpr]-(gpr:Root)
OPTIONAL MATCH (rx)-[:has_crossreference_to]-(cr)-[:has_ec_number]-(ec)
WITH rx,r,cr,ec,gpr,
COLLECT(DISTINCT {specie: l.cpdEntry, stoichiometry: l.stoichiometry}) as reacts
UNWIND reacts as rcts
WITH rx,r,cr,ec,gpr, rcts, reacts
WHERE rcts.specie =~ {searchText} OR rx.entry =~ {searchText} OR
rx.name =~ {searchText} OR (ec.entry IS NOT NULL AND
ec.entry =~ {searchText}) OR rx.geneRule =~ {searchText}
RETURN {entry: rx.entry,
reactants: reacts,
products:COLLECT(DISTINCT {specie: r.cpdEntry,
stoichiometry: r.stoichiometry}),
orientation: rx.orientation, name: rx.name, ecnumber: ec.entry,
gpr_rule: rx.geneRule, gpr_normalized: gpr.normalized_rule}
ORDER BY ' + reactionsTableMap[sortCol] + ' ' + order + ' SKIP {offset} LIMIT {number}'
The easiest is to store the result from Neo4j as a JSON string in redis, and set an expiry time on that key. Now when you need to retrieve the data, you check if the key is there, then redis serves as a cache, and if the key is not there, you ask Neo4j, store the result in redis and returns that to your Node.js program.
Pseudo code since I don't know Node.js specifics regarding Neo4J and Redis:
var result = redis.get("Record:123")
if (result == null) {
result = neo4j.query("...");
redis.setex("Record:123", toJson(result), 10); // set with expiry time
}
return result;
Redis will handle the expiry so you don't have to.
If you want to store them all, you can store them in a LIST or a ZSET (sorted set by record Id for example) and just call redis LRANGE/ZRANGE to retrieve a part of that list/set
Example with list:
var exist = redis.exist("Records"); // check if something stored in redis
if (!exist) {
var queryResult = neo4j.query("...); // get a list of results from neo4j
queryResult.foreach(result => redis.lpush("Records", toJson(result))); // add the results in the redis list
}
return redis.lrange("Records", 0, 50); // get the 50 first items
Now just iterate on that using the two parameters of redis.lrange by getting the ten items then the next ten.
You can also call redis EXPIRE to set an expiry time on the redis list.
I'm trying to create a show function which needs to access to two documents: The document in 'doc' reference and another document called 'users'
My function looks like:
function(doc,req){
var friends = doc.friends;
var listFriends = [];
for(int i = 0; i<friends.length; i++){
var phone = friends[i].phone;
if(users[phone] != "" ){
listFriends.push(users[phone]);
}
}
return JSON.stringify(listFriends);
}
I'm not an expert nor javascript neither couchdb. My question is, Is it possible to access to the second document (users) in a similar way like in the code? So far it returns a compilation error.
Thanks
You can only access one document in a CouchDB show function. You could look at using a list function, which works on view results instead of documents.
Create a view where the two documents collate together (appear side-by-side in the view order) and you achieve an effect pretty close to what you wanted to achieve with the show function.
Why is no SQL being generated when I run my Nhibernate 3 query?
public IQueryable<Chapter> FindAllChapters()
{
using (ISession session = NHibernateHelper.OpenSession())
{
var chapters = session.QueryOver<Chapter>().List();
return chapters.AsQueryable();
}
}
If I run the query below I can see that the SQL that gets created.
public IQueryable<Chapter> FindAllChapters()
{
using (ISession session = NHibernateHelper.OpenSession())
{
var resultDTOs = session.CreateSQLQuery("SELECT Title FROM Chapter")
.AddScalar("Title", NHibernateUtil.String)
.List();
// Convert resultDTOs into IQueryable<Chapter>
}
}
Linq to NHibernate (like Linq to entities) uses delayed execution. You are returning IQueryable<Chapter> which means that you might add further filtering before using the data, so no query is executed.
If you called .ToList() or .List() (i forget which is in the API), then it would actually produce data and execute the query.
In other words, right now you have an unexecuted query.
Added: Also use Query() not QueryOver(). QueryOver is like detached criteria.
For more info, google "delayed execution linq" for articles like this