MongoDB
> db.users.ensureIndex({ score: 1 })
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.users"
},
{
"v" : 1,
"key" : {
"score" : 1
},
"name" : "score_1",
"ns" : "mydb.users"
}
]
> db.users.dropIndex('score_1')
{ "nIndexesWas" : 2, "ok" : 1 }
> db.users.ensureIndex({ name: 1 }, { unique: 1 })
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.users.insert({ name: 'user-0' })
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb.users.$name_1 dup key: { : \"user-0\" }"
}
})
> db.users.update({ name: 'user-0' }, { $set: {score: 100} })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.users.update({ name: 'user-0' }, { $unset: {score: ''} })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.update({ name: 'user-1' }, { $inc: {score: 10} })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.update({ name: 'user-1' }, { $rename: {score: 'myscore'} })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.update({team: 'team-0' }, {$inc: {score: 10}}, {multi: true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.users.update({name:'butcher'}, {name:'butcher', team:'dot', score: 350}, {upsert:true});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.remove({name:'butcher'})
WriteResult({ "nRemoved" : 1 })
$ mongodump -d mydb
$ mongorestore --drop