Search code examples
mongodbphp-mongodbnosql

MongoDB dynamic values


So, maybe I've understood something wrong, because I'm from the SQL world: I have a collection with goods:

_id: "0234weasq43rq",
title: "Panasonic",
descr: "blah blah",
price: 132,
specifications {
   weight: 135,
   color: "black",
   type: "LCD",
   diagonal: 50
}

_id: "0234weasq43rq",
title: "Samsung Galaxy Tab",
descr: "blah blah",
price: 132,
specifications {
   weight: 135,
   color: "white",
   standard: "GSM",
   wifi: "Yes"
}

Can I have different specifications for different categories of goods (e.g. phones and computers)? Are there any problems finding goods using a query like goods.find(specifications { diagonal: 50 }), if not all of goods have a "diagonal" specification?

Thanks!


Solution

  • Yes, your documents, even in the same collection, may have completely different structures. In your example, the query would return just the goods that have diagonal = 50, and omit any documents that don't have diagonal defined (or that have it defined but it is not 50, obviously)