简体   繁体   中英

mongodb userid form data linked/embedded in different collections

I am working on mongodb and i was wondering whether it is possible to link some data to a userid in different mongodb collections?

when i submit the form each one of them has a different consecutive user id. not the same

the coding so far i 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 my perspective i reckon, the coding is fine. can anyone point out what is wrong with the coding in order to achieve what i am trying to do: linking data to a particular userid in different collections?

help much appreciated.

Your schema design seems to me like it was a relational schema put on a document oriented database.

To answer your question it is perfectly ok to do something like this

{ "_id": <someOid>,
  "name": "Bob",
  "city": "DaCity",
  "car" : <oIdOfACar>
}

However, since there are no joins in mongoLand, this means that you have to do two queries in order to have all information about Bob and his car. This approach has the advantage of minimizing redundancy.

Depending on what your application is all about, you might want to take an approach which is called embedding:

{ "_id": <someOid>,
  "name": "Bob",
  "city": "DaCity",
  "car" : {"name":"De Lorean", "model":"DMC-12"}
}

which has redundancy, but this may be an advantage, for example when your cars collection only should include newer models or some models might get deleted over time. Then, you can still access the users car despite it is not in the car collection any more. So you would use the car collection (or something like carqueryapi.com ) for providing valid input data and save your users as complete, non linked documents. In case you want to find out how many users drive a De Lorean DMC-12, you could do a query in mongo shell syntax like

 db.users.find({"car.name":"De Lorean","car.model":"DMC-12"}).count()

or even query for the actual users with

 db.users.find({"cars": { $elemMatch :{ "name":"De Lorean","model":"DMC-12"}}})

if a user can have multiple cars and you save them as an array called cars .

Plus you do not have to query for the car each time you want to access it from a user document. Even some analysis is possible:

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

But as I said: How to design your schema heavily depends on what your app is supposed to do.

In general, you might want to have a deep look into mongoDB's docs about data modelling .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM