Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am comparing MongoDB with PostgreSQL query abilities. I struggle a bit with finding simple equivalent for the following MongoDB queries. What are the equivalent commands in PostgreSQL JSONB to MongoDB's:

Single Element Satisfies the Criteria
db.inventory.find( { memos: { $elemMatch: { memo: 'on time', by: 'shipping' } } } )

Combination of Elements Satisfies the Criteria
db.inventory.find( { 'memos.memo': 'on time', 'memos.by': 'shipping' } )

From: https://docs.mongodb.org/manual/tutorial/query-documents/#array-of-embedded-documents

share|improve this question
SELECT ($${
   "_id": 100,
   "type": "food",
   "item": "xyz",
   "qty": 25,
   "price": 2.5,
   "ratings": [ 5, 8, 9 ],
   "memos": [ { "memo": "on time", "by": "shipping" }, { "memo": "approved", "by": "billing" } ]
}$$::jsonb)@>'{"memos":[{"by":"shipping"}]}';
 ?column? 
----------
 t

Note that that only works with jsonb.

You'll probably find the documentation on json indexing useful too.

share|improve this answer

Assuming the table looks like:

create table memos (data jsonb);

This should work:

select data from memos where memos data->'memo' = 'on time' and data->'by' = 'shipping';

http://www.postgresql.org/docs/9.5/static/functions-json.html

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.