php - mongodb userid form data linked/embedded in different collections -


i working on mongodb , wondering whether possible link data userid in different mongodb collections?

when submit form each 1 of them has different consecutive user id. not same

the coding far have is:

<?php   $content['_id'] = new mongoid(); $m = new mongo();  $db = $m->learning;      $users = $db->users;     $products = $db->products;  if ( isset($_post['name'])) {     $users->insert( array(             "_id" => $content['_id']     "name" => $_post['name'],     "city" => $_post['city'],     ));     }  if ( isset($_post['car'])) {     $products->insert( array(     "_id" => $content['_id'],     "car name" => $_post['car'],     ));     } 

from perspective reckon, coding fine. can point out wrong coding in order achieve trying do: linking data particular userid in different collections?

help appreciated.

your schema design seems me relational schema put on document oriented database.

to answer question ok this

{ "_id": <someoid>,   "name": "bob",   "city": "dacity",   "car" : <oidofacar> } 

however, since there no joins in mongoland, means have 2 queries in order have information bob , car. approach has advantage of minimizing redundancy.

depending on application about, might want take approach called embedding:

{ "_id": <someoid>,   "name": "bob",   "city": "dacity",   "car" : {"name":"de lorean", "model":"dmc-12"} } 

which has redundancy, may advantage, example when cars collection should include newer models or models might deleted on time. then, can still access users car despite not in car collection more. use car collection (or carqueryapi.com) providing valid input data , save users complete, non linked documents. in case want find out how many users drive de lorean dmc-12, query in mongo shell syntax

 db.users.find({"car.name":"de lorean","car.model":"dmc-12"}).count() 

or query actual users with

 db.users.find({"cars": { $elemmatch :{ "name":"de lorean","model":"dmc-12"}}}) 

if user can have multiple cars , save them array called cars.

plus not have query car each time want access user document. analysis possible:

db.users.find({"car.name":"toyota"}).count() 

but said: how design schema heavily depends on app supposed do.

in general, might want have deep mongodb's docs data modelling.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -