quarta-feira, 30 de novembro de 2022

[Mongo DB] - Basic sort and read operations (or "R" from CRUD)

Hello all

In the last post (here), I have talked about create operations and running inserts, or the "C" from CRUD.

Today, I'm going to talk about executing read operations in Mongo DB, the "R" in CRUD. I'll talk about sort, as well.

But remember, I'm talking about basic operations. As we know, the Mongo DB world is much bigger than this post.  

Let's get started!!!

The references are here and here.

-- List all
db.movies.find()


-- Find the first row matching the object
db.movies.findOne({trilogy: "Transformers"})


-- Limit the number of rows
db.movies.find().limit(3)


-- Count the number of rows
db.movies.find().count()


-- Where trilogy="Transformers"
db.movies.find( {trilogy: "Harry Potter"} )


-- List fields _id, codigo, sequence, year
db.movies.find( {trilogy: "Transformers"}, { codigo: 1, sequence: 1, year: 1 })


-- List fields withouth id
db.movies.find( {trilogy: "Transformers"}, { _id: 0, codigo: 1, sequence: 1, year: 1 })


-- Concat Trilogy - Sequence
db.movies.find({trilogy: "Transformers"},{ _id: 0, codigo: 1, year: 1, name: {$concat: ["$trilogy"," - ","$sequence"]} })


-- List rows with codigo are between 5 and 12
db.movies.find({ codigo: { $gt: 5, $lt: 12 } },{ _id: 0, codigo: 1, year: 1, name: {$concat: ["$trilogy"," - ","$sequence"]} })


-- List rows with codigo are between 5 and 12 and Trilogy="Transformers"
db.movies.find({ codigo: { $gt: 5, $lt: 12 }, trilogy: "Transformers"},{ _id: 0, codigo: 1, year: 1, name: {$concat: ["$trilogy"," - ","$sequence"]} })


-- List rows with codigo are between 5 and 12 and Sort - Year Desc, Name Asc
db.movies.find({ codigo: { $gt: 5, $lt: 12 } },{ _id: 0, codigo: 1, year: 1, name: {$concat: ["$trilogy"," - ","$sequence"]} }).sort({"year": -1, "name": 1 })


That's all folks!!!

Yes, the reading operations are easy!!! 

I hope that it helps you!!!

Regards
Mario 

Nenhum comentário:

Postar um comentário

Isso te ajudou? Comente...

Postagem em destaque

[ORACLE] Batch change EDITIONABLE property.

Hello everyone. Hope you're doing well! Today, I have a simple case.   A test database had many database objects with the EDITIONABLE pr...