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!!!
-- Find the first row matching the object
db.movies.findOne({trilogy: "Transformers"})
db.movies.findOne({trilogy: "Transformers"})
-- Limit the number of rows
db.movies.find().limit(3)
db.movies.find().limit(3)
-- Count the number of rows
db.movies.find().count()
db.movies.find().count()
-- Where trilogy="Transformers"
db.movies.find( {trilogy: "Harry Potter"} )
db.movies.find( {trilogy: "Harry Potter"} )
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 })
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"]} })
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"]} })
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...