-
Notifications
You must be signed in to change notification settings - Fork 5
Labo 10 solutions
Jean-Philippe Caissy edited this page Nov 20, 2013
·
1 revision
MongoDB shell version: 2.0.6
connecting to: test
> use labo7
switched to db labo7
> db.produit.insert({'nom': 'Macbook Pro', 'fabriquant': 'Apple', 'prix': 1299, options: ['Intel Core i5', 'Retina Display', 'Long battery life']})
> db.produit.insert({'nom': 'Macbook Pro Air', 'fabriquant': 'Apple', 'prix': 1099.99, options: ['Intel Core i7', 'SSD', 'Long battery life']})
> db.produit.insert({'nom': 'Thinkpad X230', 'fabriquant': 'Lenovo', 'prix': 999.99, 'ultrabook': true, options: ['Intel Core i5', 'SSD', 'Long battery life']})
> db.produit.find()
{ "_id" : ObjectId("51ddf0d334de89241d03e21e"), "nom" : "Macbook Pro", "fabriquant" : "Apple", "prix" : 1299, "options" : [ "Intel Core i5", "Retina Display", "Long battery life" ] }
{ "_id" : ObjectId("51ddf0f234de89241d03e21f"), "nom" : "Macbook Pro Air", "fabriquant" : "Apple", "prix" : 1099.99, "options" : [ "Intel Core i7", "SSD", "Long battery life" ] }
{ "_id" : ObjectId("51ddf12834de89241d03e220"), "nom" : "Thinkpad X230", "fabriquant" : "Lenovo", "prix" : 999.99, "ultrabook" : true, "options" : [ "Intel Core i5", "SSD", "Long battery life" ] }
> db.produit.find()[0]
{
"_id" : ObjectId("51ddf0d334de89241d03e21e"),
"nom" : "Macbook Pro",
"fabriquant" : "Apple",
"prix" : 1299,
"options" : [
"Intel Core i5",
"Retina Display",
"Long battery life"
]
}
> db.produit.find({'_id': ObjectId("51ddf12834de89241d03e220")})
{ "_id" : ObjectId("51ddf12834de89241d03e220"), "nom" : "Thinkpad X230", "fabriquant" : "Lenovo", "prix" : 999.99, "ultrabook" : true, "options" : [ "Intel Core i5", "SSD", "Long battery life" ] }
> db.produit.find({'prix': {$gt: 1200}})
{ "_id" : ObjectId("51ddf0d334de89241d03e21e"), "nom" : "Macbook Pro", "fabriquant" : "Apple", "prix" : 1299, "options" : [ "Intel Core i5", "Retina Display", "Long battery life" ] }
> db.produit.find({'ultrabook': true})
{ "_id" : ObjectId("51ddf12834de89241d03e220"), "nom" : "Thinkpad X230", "fabriquant" : "Lenovo", "prix" : 999.99, "ultrabook" : true, "options" : [ "Intel Core i5", "SSD", "Long battery life" ] }
> db.produit.find({'nom': /Macbook/})[0]
{
"_id" : ObjectId("51ddf0d334de89241d03e21e"),
"nom" : "Macbook Pro",
"fabriquant" : "Apple",
"prix" : 1299,
"options" : [
"Intel Core i5",
"Retina Display",
"Long battery life"
]
}
> db.produit.find({'nom': /^Macbook/})
{ "_id" : ObjectId("51ddf0d334de89241d03e21e"), "nom" : "Macbook Pro", "fabriquant" : "Apple", "prix" : 1299, "options" : [ "Intel Core i5", "Retina Display", "Long battery life" ] }
{ "_id" : ObjectId("51ddf0f234de89241d03e21f"), "nom" : "Macbook Pro Air", "fabriquant" : "Apple", "prix" : 1099.99, "options" : [ "Intel Core i7", "SSD", "Long battery life" ] }
1. Supprimer les deux produits dont le fabricant est Apple, car on le sait bien, les Thinkpad sont meilleurs ;-)
> db.produit.remove({'fabriquant': 'Apple'})
> db.produit.remove({'_id': ObjectId("51ddf12834de89241d03e220")})
> db.facture.insert({
"numero_facture" : "10012A",
"date" : new Date("Jul 4, 2013"),
"client" : {
"nom" : "Jean-Philippe Caissy",
"email" : "[email protected]"
},
"produits" : [
{
"code" : "MACBOOKAIR",
"nom" : "Macbook Air",
"prix" : 999.99,
"quantite" : 1
},
{
"code" : "APPLESUPPORT",
"nom" : "AppleCare 1 an",
"prix" : 149.99,
"quantite" : 1
}
],
"total" : 1149.98
})
> db.facture.insert({
"numero_facture" : "10013A",
"date" : new Date("Jul 5, 2013"),
"numero_facture" : "10013A",
"client" : {
"nom" : "Jacques Berger",
"email" : "[email protected]"
},
"produits" : [
{
"code" : "LENOVOX230",
"nom" : "Lenovo Thinkpad X230",
"prix" : 899.99,
"quantite" : 1
},
],
"total" : 899.99
})
> db.facture.find({'numero_facture': '10013A'})
{ "_id" : ObjectId("51de023dcb544aade2fd7cb8"), "numero_facture" : "10013A", "date" : ISODate("2013-07-05T04:00:00Z"), "client" : { "nom" : "Jacques Berger", "email" : "[email protected]" }, "produits" : [ { "code" : "LENOVOX230", "nom" : "Lenovo Thinkpad X230", "prix" : 899.99, "quantite" : 1 } ], "total" : 899.99 }
2. Modifier la facture 10012A en changeant la date pour le 2013-07-03 et le courriel du contact pour [email protected]
> db.facture.update({'numero_facture': '10012A'}, { $set: {'date': new Date('Jul 3, 2013'), 'client.email': '[email protected]'}})
> db.facture.find({produits: { $elemMatch: {'code': 'LENOVOX230'}}})
{ "_id" : ObjectId("51de039acb544aade2fd7cbb"), "numero_facture" : "10013A", "date" : ISODate("2013-07-05T04:00:00Z"), "client" : { "nom" : "Jacques Berger", "email" : "[email protected]" }, "produits" : [ { "code" : "LENOVOX230", "nom" : "Lenovo Thinkpad X230", "prix" : 899.99, "quantite" : 1 } ], "total" : 899.99 }
> db.facture.remove({'numero_facture': '10012A'})