x = { "a" : 1 };
y = "a";
x[y]++;
print(x.a);
2
BSON Introduced
Mongo shell, inserting docs
Mongo shell, introdution to findOne
Mongo shell, querying using field selection
Querying using $gt and $lt
Inequalities on strings
Using regexes, $exists, $type
Using $or
Using $and
db.scores.find( { score : { $gt : 50 }, score : { $lt : 60 } } );
Querying inside arrays
db.products.find( { tags : "shiny" } );
Using $in and $all
db.users.find( { friends : { $all : [ "Joe" , "Bob" ] }, favorites : { $in : [ "running" , "pickles" ] } } )
Queries with dot notation
{ product : "Super Duper-o-phonic",
price : 100000000000,
reviews : [ { user : "fred", comment : "Great!" , rating : 5 },
{ user : "tom" , comment : "I agree with Fred, somewhat!" , rating : 4 } ],
... }
Write a query that finds all products that cost more than 10,000 and that have a rating of 5 or better.Querying, cursors
{
"_id" : ObjectId("50844162cb4cf4564b4694f8"),
"student" : 0,
"type" : "exam",
"score" : 75
}
Write a query that retrieves exam documents, sorted by score in
descending order, skipping the first 50 and showing only the next 20.Counting results
Wholesale Updating of a Document
{ "_id" : "Texas", "population" : 2500000, "land_locked" : 1 }
and you issued the query:db.foo.update({_id:"Texas"},{population:30000000})
Using the $set command
{'username':'splunker', 'country':'US', 'phone':'718-343-3433'}
in the collection users, write the shell command for updating the country to 'RU' for only this user.Using the $unset command
{'username':'jimmy', favorite_color:'blue', interests:['debating', 'politics']}
Using $push, $pop, $pull, $pushAll, $pullAll, $addToSet
{ _id : "Mike", interests : [ "chess", "botany" ] }
What will the result of the following updates be?db.friends.update( { _id : "Mike" }, { $push : { interests : "skydiving" } } );
db.friends.update( { _id : "Mike" }, { $pop : { interests : -1 } } );
db.friends.update( { _id : "Mike" }, { $addToSet : { interests : "skydiving" } } );
db.friends.update( { _id : "Mike" }, { $pushAll: { interests : [ "skydiving" , "skiing" ] } } );
Multi-update
{
"_id" : ObjectId("50844162cb4cf4564b4694f8"),
"student" : 0,
"type" : "exam",
"score" : 75
}
How would you give every record whose score was less than 70 an extra 20 points?Removing data
{
"_id" : ObjectId("50844162cb4cf4564b4694f8"),
"student" : 0,
"type" : "exam",
"score" : 75
}
How would you delete every record whose score was less than 60?Java driver: representing documents
{
"_id" : "user1",
"interests" : [ "basketball", "drumming"]
}
Java driver: insert
MongoClient client = new MongoClient();
DB db = client.getDB("school");
DBCollection people = db.getCollection("people");
DBObject doc = new BasicDBObject("name", "Andrew Erlichson")
.append("company", "10gen");
try {
people.insert(doc); // first insert
doc.removeField("_id"); // remove the "_id" field
people.insert(doc); // second insert
} catch (Exception e) {
e.printStackTrace();
}
Java driver: find, findOne, count
MongoClient client = new MongoClient();
DB db = client.getDB("school");
DBCollection people = db.getCollection("people");
DBObject doc;
xxxx
System.out.println(doc);
Please enter the simplest one line of Java code that would be needed in
place of xxxx to make it print one document from the people collection.Java driver: query criteria
Java driver: field selection
Java driver: dot notation
DBObject findOneUrlByMediaType(DBCollection videos, String mediaType) {
DBObject query = new BasicDBObject("media.type", mediaType);
DBObject projection = new BasicDBObject("media.url", true);
return videos.findOne(query, projection);
}
Java driver: sort, skip and limit
{ "_id" : 0, "value" : 10 }
{ "_id" : 1, "value" : 5 }
{ "_id" : 2, "value" : 7 }
{ "_id" : 3, "value" : 20 }
If you performed the following query in the Java driver:collection.find().sort(new BasicDBObject("value", -1)).skip(2).limit(1);
which document would be returned?Java driver: update and remove
# update using $set
scores.update(new BasicDBObject("_id", 1), xxxx);
No hay comentarios:
Publicar un comentario