node.js - Is there a way to create a "stored procedure" call multi queries in single query at mongodb(mongoose) -


Is there a way to create "stored procedure" in mongodb?

For example, to use a query, once the monodod, but are actually calling more than one collection? (Use mongos to call Mongod)

I know that Mongodb has it but it is different, I do not think the server side function is capable of calling the collection. For example if I have an example.

1. Example one (relational collection)

  mycollection1.findOne ({_ id: id}, function (error, result1) {mycollection2.findOne ({_ id: result1.id}, function ( Mistake, result 2) {Mycollection3.findOne ({_ id: result2.id}, function (mistake, result 3) {return {};});});});  

2 Example Two (any relational collection)

  mycollection1.findOne ({_ id: id}, function (error, result1) {mycollection2 .findOne ({}, function (fault, result 2) {mycollection3 .findOne ({}, function (mistake, result 3) {var someDDTTTTTUU = {data1: result1, data2: result2, data3: result3 }; Return some datataedisplay;});});});  

I am using Mongoos on the express.js server, currently call three times the example mentioned above. Is there a way to call once?

/ Code>. There is no way around it. You have to do multiple queries at the application level. What are you doing, the way to go is the risk of non-relational databases. It may be that if you need to make these calls frequently, you will be better off the relational database or it would be good to reconsider your schema.

Mongodeib is not friendly to the relationship but as you are using mongoose , if you have any relationships, you can find references in many collections of documents. And use a clean option called populate , which receives data from other collections

Nevertheless, Monza will do two-mongo queries internally < / P>

This option defines relationships as a b Electricity is very easy.

Check.

Populat can not be used for more than one level at a time.

  mycollection1.findOne ({_ id: id}). Populate ('id', null, ',' & lt; Collection 2 model name & gt; function (mistake, result 1) {//result1.id, 1 .id.populate instead of ID result in your result2 ( 'Id.id', null, '& lt; Collection 3 model & gt;', function (mistake, result) {//result1.id.id now your results are in question})});  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -