De la siguiente colección: productos, realizar una consulta para obtener los documentos cuyo precio de compra es superior a precio de venta.
id categoria pcompra pventa estado
1 Pantalon 400 300 a
2 Camisa 300 400 a
3 Gorra 200 100 a
4 Gafas 100 200 b
Hello @Mario_Orozco_Gonzalez, welcome to the MongoDB forum!
You can run this query from mongo
shell, to get the desired result:
// Create the collection with documents
db.products.insertMany([
{ id: 1, category: "Pantalon", buy: 400, sell: 300, status: "a" },
{ id: 2, category: "Camisa", buy: 300, sell: 400, status: "a" },
{ id: 3, category: "Gorra ", buy: 200, sell: 100, status: "a" },
{ id: 4, category: "Gafas ", buy: 100, sell: 200, status: "a" }
])
// Query the collection
db.products.find( { $expr: { $gt: [ "$buy", "$sell" ] } } )
Please refer the MongoDB Manual about the commands and operators used in the above query.
2 Likes