Jakob
Jakob

Reputation: 4854

Storing and querying JSON from a database

I've heard about MongoDB, but I'm not sure I fully understand the concept.

If I have multiple JSON objects stored in MongoDB:

[{"id": "peter",
  "age": "12",
  "gender": "male"},
 {"id": "gemma",
  "age": "12",
  "gender": "female"},
 {"id": "paul",
  "age": "13",
  "gender": "male"}]

How would I be able to query all JSON objects with age >= 12?

Upvotes: 43

Views: 93519

Answers (6)

Keshav Murthy
Keshav Murthy

Reputation: 134

A Couchbase N1QL query will look like this:

SELECT * FROM people WHERE age >= 12;

See https://query-tutorial.couchbase.com/tutorial/#1 for more details.

Upvotes: 0

charles ross
charles ross

Reputation: 572

A mongodb shell query for that might look like this:

db.people.find({"age": {gt:12 }});

Upvotes: 3

Mike Brant
Mike Brant

Reputation: 71384

First of all, understand that JSON is just a serialization technique. In and of itself, this serialization method probably should not determine your persistence medium. Looking at your question on the surface, it seems like what you are looking for is a typical relational storage database where you can use SQL to query against your data in a flexible manner.

Serializing/deserializing JSON data for storage into or for presentation after retrieval from such a relational database is trivial in pretty much any programming language.

Now if you truly need to store various snippets of JSON documents (or any other sort of document) that don't really have a fixed structure, that is really when you typically would start looking at a NoSQL type of solution such as MongoDB. One other possible such scenario for using the more popular NoSQL databases is when you are dealing with massive amounts of data and need to scale horizontally (i.e. the data is so large you need to scale the database across multiple servers). Many NoSQL systems make this much easier to do than traditional relational DB's. Of course in such a scenario, you would then need to evaluate those tools based on the functionality they provide in allowing you to read, write, and query data in the most useful manner for your use case(s).

Upvotes: 51

Sophia Feng
Sophia Feng

Reputation: 1003

Starting from 5.7, MySQL now has native JSON datatype. Assuming your table is called students and the JSON column is called student, in MySQL 5.7 your select can be written as

SELECT * FROM students WHERE JSON_EXTRACT(student, '$.age') = 12;

Upvotes: 19

Manu Viswam
Manu Viswam

Reputation: 1646

MongoDB stores data in BSON format which is similar to JSON. You can store data in JSON format and can make a query on any field. You can even index on a particular field which is used for major queries.

You are not limited to MongoDB, Seeing your question i think any of the Document-store will suit your needs. You read more here :- http://en.wikipedia.org/wiki/Document-oriented_database

Upvotes: 15

tskuzzy
tskuzzy

Reputation: 36476

If all your objects have the same fields (id, age, gender, etc.) then you should store the objects as rows of some relational database (e.g. MySQL).

Upvotes: 7

Related Questions