lungo icon indicating copy to clipboard operation
lungo copied to clipboard

Dump or export

Open 89z opened this issue 5 years ago • 2 comments

With other projects, you can dump to SQL, or export to CSV:

Is something like this possible with Lungo?

89z avatar Dec 11 '20 05:12 89z

The FileStore storage adapter currently stores the full database as one BSON file. If you count BSON as an export format then the storage format would satisfy this already. Otherwise there is no other export or dump function implemented ATM.

In the future, it would be interesting to support the MongoDB dump format either directly with a storage adapter or through methods that dump or restore to/from that format.

256dpi avatar Dec 11 '20 10:12 256dpi

Here is a simpler method:

package main

import (
   "encoding/json"
   "github.com/256dpi/lungo"
   "go.mongodb.org/mongo-driver/bson"
   "log"
   "os"
)

type post struct {
   Title string `bson:"title"`
}

func main() {
   opts := lungo.Options{
      Store: lungo.NewFileStore("lungo.db", os.ModePerm),
   }
   client, engine, e := lungo.Open(nil, opts)
   if e != nil {
      log.Fatal(e)
   }
   defer engine.Close()
   csr, e := client.Database("month").Collection("March").Find(nil, bson.M{})
   if e != nil {
      log.Fatal(e)
   }
   var posts []post
   csr.All(nil, &posts)
   create_o, e := os.Create("post.json")
   if e != nil {
      log.Fatal(e)
   }
   json_o := json.NewEncoder(create_o)
   json_o.SetIndent("", " ")
   json_o.Encode(posts)
}

89z avatar Dec 31 '20 05:12 89z