I would like to convert a mongo collection to capped using gopkg.in/mgo.v2
.
I am able to create a capped collection from scratch - as follows:
# Create a Capped Collection
sess.DB("").C("my_collection").Create(&mgo.CollectionInfo{Capped: true, MaxBytes: tenMB, MaxDocs: 10})
I can't work out how to get the stats for an existing collection or how to run convertToCapped
command.
Step 1 - Get Collection Stats:
# Mongo
db.getCollection('my_collection').stats();
# mgo // I need to find out how to do this.
Step 2 - Convert to Capped
# Mongo
db.runCommand({"convertToCapped": "my_collection", size: 1000000});
# mgo // I need to find out how to do this.
if err := sess.DB("").Run(bson.D{{"convertToCapped", "my_collection"}, {"size", "1000"}}, nil); err != nil {
println(err.Error()) // invalid command spec
os.Exit(1)
}
This is not part of the mgo
package, but it is available as a runnable MongoDB command: collStats
.
Running collStats
with mgo
:
var doc bson.M
err := mgr.sess.DB("").Run(bson.D{
{Name: "collStats", Value: "my_collection"},
}, &doc)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Result:", doc)
fmt.Println("Capped:", doc["capped"])
// Acquire capped info as a bool value:
capped, ok := doc["capped"].(bool)
if ok {
fmt.Println("Capped bool value:", capped)
}
Example output (veeeeeeery long so chunked):
Result: map[ok:1 size:36 storageSize:4096 totalIndexSize:4096 ...chunked...]
Capped: true
Capped bool value: true
The simple problem with your attempt is that the value of the "size"
parameter must be a number and not a string
. Quoting from the convertToCapped
doc:
The command has the following syntax:
{ convertToCapped: <collection>, size: <capped size> }
convertToCapped
takes an existing collection (<collection>
) and transforms it into a capped collection with a maximum size in bytes, specified by thesize
argument (<capped size>
).
Correct version:
var doc bson.M
err := mgr.sess.DB("").Run(bson.D{
{Name: "convertToCapped", Value: "my_collection"},
{Name: "size", Value: 1<<20}, // 1 MB
}, &doc)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Result:", doc)
Example output:
Result: map[ok:1]