UE « Systèmes d'Information Tactiques »
Master 2 Génie Logiciel
ISTIC, Université de Rennes 1
Année 2015-2016, second semestre
Emmanuel Caruyer
CR CNRS
Emmanuel.Caruyer@irisa.fr
{
_id: ObjectID('4bd9e8e17cefd644108961bb'),
titre: "La Renault Twizy, entre scooter et citadine",
auteur: "ecaruyer",
categories: ["automobile", "écologie", "zéro émission"],
commentaires: [
{
visiteur: "david35",
texte: "Merci pour cet article !"
},
{
visiteur: "melody_nelson",
texte: "Cette voiture est top, mais pêche encore par son autonomie..."
}
],
texte: "La citadine <strong>Renault Twizy</strong> [...]"
}
mongod.exemongo.exeuse <nom_de_la_base>
db.<nom_de_la_collection>.insert(<document>)
db.<nom_de_la_collection>.find([paramètres])
ecaruyer@victoria ~ $ mongo
MongoDB shell version: 2.4.9
connecting to: test
> use bibliographie
switched to db bibliographie
> document = {
... auteurs: ["Antoine Dupont", "Frédéric Petit"],
... titre: "Databases for the next generation",
... editeur: "University Press",
... pages: 525,
... categories: ["informatique", "vulgarisation", "technologies web"]
... }
> db.articles.insert(document)
> db.articles.find()
{ "_id" : ObjectId("569c5dac1dd328fd9d406fb0"), "auteurs" : [ "Antoine
Dupont", "Frédéric Petit" ], "titre" : "Databases for the next generation",
"editeur" : "University Press", "pages" : 525, "categories" : [
"informatique", "vulgarisation"] }
db.articles.find({auteurs: "Antoine Dupont"})
.update({"$addToSet": {"categories": "technologies web"}})
db.articles.find({auteurs: "Antoine Dupont"})
.update({"$addToSet": {"categories": "technologies web"}}, false, true)
db.articles.remove({auteurs: "Antoine Dupont"})
// Create a client to connect to the MongoDB server on localhost
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
// Connect to a specific database.
// Note: if "mydb" does not exist, it'll be create on the first write.
MongoDatabase database = mongoClient.getDatabase("bibliographie");
// Retrieve (or create) the collection "articles".
MongoCollection collection = database.getCollection("article");
// Make a document and insert it
Document doc = new Document("auteurs", Arrays.asList("Antoine Dupont", "Frédéric Petit"))
.append("titre", "Databases for the next generation")
.append("editeur", "University Press")
.append("categories", Arrays.asList("informatique", "vulgarisation", "technologies web"));
collection.insertOne(doc);
// Query the collection
Document myDoc = collection.find().first();
db.articles.find({editeur: "University Press"})
db.articles.find({pages: {"$gt": 500}})
db.articles.find({auteurs: {"$in": ["Eric Legrand", "Antoine Dupont"]}})
db.articles.find({auteurs: {$not: /^F/}})
db.billets.find({"categories": "automobile",
"commentaires.visiteur": "melody_nelson"}})
db.billets.find({"$or": [{"categories": "automobile"},
{"commentaires.visiteur": "melody_nelson"}]})
db.articles.find({editeur: "University Press"},
{auteurs: 1})
db.articles.find({}).sort({titre: -1})
explain()> db.articles.find({"auteurs.nom": "Dupont"}).explain()
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 2,
"nscanned" : 2,
"nscannedObjectsAllPlans" : 2,
"nscannedAllPlans" : 2,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
},
"server" : "victoria:27017"
}
ensureIndex> db.articles.ensureIndex({"auteurs.nom": 1})
> db.articles.find({"auteurs.nom": "Dupont"}).explain()
{
"cursor" : "BtreeCursor auteurs.nom_1",
"isMultiKey" : true,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 1,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"auteurs.nom" : [
[
"Dupont",
"Dupont"
]
]
},
"server" : "victoria:27017"
}
{
"_id" : ObjectId("4d291187888cec7267e55d24"),
"city" : "New York City",
"loc" : {
"lon" : -73.9996
"lat" : 40.7402,
},
"state" : "New York",
"zip" : 10011
}
> db.zips.ensureIndex({loc: '2d'})
> db.zips.find({'loc': {$near: [ -73.977842, 40.752315 ]}}).limit(3)
> center = [-73.977842, 40.752315]
> radius = 0.011
> db.zips.find({loc: {$within: {$center: [ center, radius ] }}})
> lower_left = [-73.977842, 40.752315]
> upper_right = [-73.923649, 40.762925]
> db.zips.find({loc: {$within:
{$box: [ lower_left, upper_right ] }}})