0

I am trying to learn databases and nodejs.

I want to ask how can I translate the following from mongod to postgresql using nodejs.

//fill QueryString
collection.find({"myID" : {$in:QueryString} },{}).toArray(function(err, Stuff) { 

.....

if ( .... ) {

collection.update(
      { "myID" : req.body.id },
      {$set : { "myField" : req.body.fileid }}, 
      function(err, resultNew) {
..
}//end of update
}//end of find

I tried :

//fill QueryString
for (var i=0; i<req.body.my_ids.length; i++) {
    QueryString[i] = req.body.my_ids[i]; 
}

pg.connect(conString, function(err, client, done) {

        if (err) return console.error('error fetching client from pool', err);


       client.query("SELECT FROM mytable WHERE myID = ANY  array_to_string(QueryString::int[],', ') ", function(err, Stuff) {
           ....

          client.query("UPDATE mytable WHERE myID = 'req.body.id' SET myField ='req.body.fileid' " , function(err,result) {

            });//end of update

            }); // end of select

I am not sure my translations.

In the first query when using mongo , the "Stuff" will be an array and I have to do some calculations with it, but in node-postgres?Will it be an array ,or I have to specify something?Right now I can't find the appropriate syntax to search in QueryString.

In the second query I am not sure at all if it is right,or how can I write it.

1 Answer 1

0

So , it seems that this works:

client.query( "SELECT * FROM mytable WHERE my_id = ANY ($1::varchar[])  ",[QueryString], function(err, Stuff) {

client.query("UPDATE mytable  SET myField ='req.body.fileid' WHERE my_id = 'req.body.id'",  function(err, result)  {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.