Skip to content
Jean-Philippe Caissy edited this page Nov 20, 2013 · 1 revision

Exercice 3

Insertion des produits

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']})

Récupérer les documents

1. Récupérer tous les produits.

> 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" ] }

2. Récupérer le premier produit

> db.produit.find()[0]
{
	"_id" : ObjectId("51ddf0d334de89241d03e21e"),
	"nom" : "Macbook Pro",
	"fabriquant" : "Apple",
	"prix" : 1299,
	"options" : [
		"Intel Core i5",
		"Retina Display",
		"Long battery life"
	]
}

3. Trouvez l'id du Thinkpad et faites la requête pour récupérer ce produit avec son id.

> 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" ] }

4. Récupérer les produits dont le prix est supérieur à 1200$

> 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" ] }

5. Récupérer le premier produit ayant le champ ultrabook à true

> 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" ] }

6. Récupérer le premier produits donc le nom contient Macbook

> 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"
	]
}

7. Récupérer les produits dont le nom commence avec Macbook

> 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" ] }

Supression

1. Supprimer les deux produits dont le fabricant est Apple, car on le sait bien, les Thinkpad sont meilleurs ;-)

> db.produit.remove({'fabriquant': 'Apple'})

2. Avec l'ID du Lenovo X230 obtenu, supprimer le.

> db.produit.remove({'_id': ObjectId("51ddf12834de89241d03e220")})

Exercice 04

> 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
})

1. Récupérer la facture avec le numéro 10013A

> 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]'}})

3. Récupérer la facture avec le produit vendu ayant un code LENOVOX230

> 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 }

4. Supprimer la facture 10012A

> db.facture.remove({'numero_facture': '10012A'})
Clone this wiki locally