Last active
December 23, 2015 17:09
-
-
Save phpusr/6666430 to your computer and use it in GitHub Desktop.
Работа с MongoDB в Groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mongotest | |
import com.mongodb.* | |
/** | |
* @author phpusr | |
* Date: 22.09.13 | |
* Time: 20:03 | |
*/ | |
/** | |
* Работа с MongoDB в Groovy | |
*/ | |
Mongo mongo = new Mongo('localhost') | |
DB db = mongo.getDB('phpusr') | |
DBCollection roleColl = db.getCollection('role') | |
roleColl.drop() | |
//Добавление Ролей | |
roleColl.insert([name: 'admin', description: 'administrator'] as BasicDBObject) | |
roleColl.insert([name: 'manager', description: 'manager'] as BasicDBObject) | |
printColl(roleColl) | |
//Добавление Пользователей к Роли | |
roleColl.update([name: 'admin'] as BasicDBObject, [$set: [users: ['Pavel', 'Ivan']]] as BasicDBObject) | |
//Изменение описания в Роли | |
roleColl.update([name: 'manager'] as BasicDBObject, [$set: [description: 'man']] as BasicDBObject) | |
printColl(roleColl) | |
//Переименование атрибута в Роли | |
roleColl.update([] as BasicDBObject, [$rename: [name: 'imya']] as BasicDBObject, false, true) | |
//Добавление нового атрибута к Роли (создастся новая Роль, т.к. по запросу ничего не найдется) | |
roleColl.update([_id: 1] as BasicDBObject, [$set: [suka: true]] as BasicDBObject, true, false) | |
printColl(roleColl) | |
/** Вывод коллекции */ | |
void printColl(DBCollection coll) { | |
println '---------------------' | |
DBCursor cursor = coll.find() | |
cursor.each { role -> println role } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment