Closed chriscantu closed 13 years ago
Hello Chris. I'm a little late, sorry.
You should use "_id" instead of "id". Bellow is an example of how to do all you want.
Later, I will work on a better way to page elements.
@Grab("com.gmongo:gmongo:0.7")
import com.gmongo.GMongo
def GMongo gmongo = new GMongo("localhost:27017")
def db = gmongo.getDB("test")
// Find By Id
db.example.drop()
db.example << [_id: 1, name: "Foo"]
db.example << [_id: "2", name: "Bar"]
db.example << [_id: 3, name: "Baz"]
assert db.example.findOne(3).name == "Baz"
assert db.example.findOne(1).name == "Foo"
assert db.example.findOne("2").name == "Bar"
// Paging
db.example.drop()
100.times {
db.example << [time: it, random: (Integer)(Math.random() * 100)]
}
def at = 0, total = db.example.find().count()
while (at < total) {
println "At page: ${at / 10}\n"
db.example.find().limit(10).skip(at).sort(random: 1).each {
println "\t-- ${it}"
}
println "\n --------------------------"
at += 10
}
Thanks Paolo for the code snippets. I found that "id" error a little while later and felt pretty dumb. =)
What did you think about the API suggestions? I feel like findById would be more programmer friendly. Also, I feel like the .limit(10).skip(1).sort(random: 1) syntax is pretty verbose. The grails framework uses a map of params for its paginating and sorting. ex: Person.list([max: 10, offset:100, sort: "asc", orderBy:"lastName"]) If no parameters are set, then it defaults to preset values. I thought this would also be more programmer friendly. We could use the same method names for the sorting. db.coll.find(queryObj, [limit: 100, skip: 10, sort:[random:1]] to make it feel similar to the original api.
Chris, I agree when u say that 'findById' is more friendly, but I am trying to keep the API similar to Java driver. I also agree about the pagination and I will work on that ASAP.
Tks.
Hi,
I wanted to update something by id with gmongo. I have tried: db.song.update([_id: "511048380364c13f5505c036"], [$set:[title: "GGGGGG"]])
It works when using another field as the key for updating, for example "title". What am I missing? Thx /Lasse
I'm a little late to the party here, but in case anyone else finds this searching for how to do this, here's some help:
In the example @svaret provided, _id is being queried against a string, but it is an ObjectId (org.bson.types.ObjectId). The example will work with this change:
db.song.update([_id: new ObjectId("511048380364c13f5505c036")], [$set:[title: "GGGGGG"]])
ObjectId has a constructor that takes the string representation and turns converts it back into an ObjectId
Hello Paulo,
I have been looking at your documentation on Github and on your site. I can't seem to find three features, pagination, sorting and find by id.
I have been able to get pagination to work by following the Java Driver syntax. However, I have not been able to get find by id to work. I tried db.collection.findOne(_id: id) but this does not return any thing. In addition, I tried to use the Java syntax db.collection.findOne( "_id", new ObjectId(id)) but that didn't work either.
Would it be possible to inject "findById" to the collection object?
Also, would it be possible to have a simpler api for paginating and sorting? I don't mind trying to hack something out and submit a patch. I was thinking of something like this db.collection.find([skip:0, limit:10, sort: "asc", orderBy: "lastName"]).
Thoughts?