예제 #1
0
// getCursor returns a cursor that can be iterated over to get all the documents
// to export, based on the options given to mongoexport. Also returns the
// associated session, so that it can be closed once the cursor is used up.
func (exp *MongoExport) getCursor() (*mgo.Iter, *mgo.Session, error) {
	sortFields := []string{}
	if exp.InputOpts != nil && exp.InputOpts.Sort != "" {
		sortD, err := getSortFromArg(exp.InputOpts.Sort)
		if err != nil {
			return nil, nil, err
		}
		sortFields, err = bsonutil.MakeSortString(sortD)
		if err != nil {
			return nil, nil, err
		}
	}

	query := map[string]interface{}{}
	if exp.InputOpts != nil && exp.InputOpts.HasQuery() {
		var err error
		content, err := exp.InputOpts.GetQuery()
		if err != nil {
			return nil, nil, err
		}
		query, err = getObjectFromByteArg(content)
		if err != nil {
			return nil, nil, err
		}
	}

	flags := 0
	if len(query) == 0 && exp.InputOpts != nil &&
		exp.InputOpts.ForceTableScan != true && exp.InputOpts.Sort == "" {
		flags = flags | db.Snapshot
	}

	session, err := exp.SessionProvider.GetSession()
	if err != nil {
		return nil, nil, err
	}

	skip := 0
	if exp.InputOpts != nil {
		skip = exp.InputOpts.Skip
	}
	limit := 0
	if exp.InputOpts != nil {
		limit = exp.InputOpts.Limit
	}

	// build the query
	q := session.DB(exp.ToolOptions.Namespace.DB).
		C(exp.ToolOptions.Namespace.Collection).Find(query).Sort(sortFields...).
		Skip(skip).Limit(limit)

	if len(exp.OutputOpts.Fields) > 0 {
		q.Select(makeFieldSelector(exp.OutputOpts.Fields))
	}

	q = db.ApplyFlags(q, session, flags)

	return q.Iter(), session, nil

}
예제 #2
0
func getDocSource(exp MongoExport) (db.DocSource, error) {
	if exp.ToolOptions.Namespace.DBPath != "" {
		shimPath, err := db.LocateShim()
		if err != nil {
			return nil, err
		}
		bsonTool := db.StorageShim{
			DBPath:     exp.ToolOptions.Namespace.DBPath,
			Database:   exp.ToolOptions.Namespace.DB,
			Collection: exp.ToolOptions.Namespace.Collection,
			Query:      exp.InputOpts.Query,
			Skip:       exp.InputOpts.Skip,
			Limit:      exp.InputOpts.Limit,
			ShimPath:   shimPath,
		}

		iter, _, err := bsonTool.Open()
		if err != nil {
			return nil, err
		}
		return db.NewDecodedBSONSource(iter), nil
	}

	sessionProvider, err := db.InitSessionProvider(exp.ToolOptions)
	if err != nil {
		return nil, err
	}
	session, err := sessionProvider.GetSession()
	if err != nil {
		return nil, err
	}
	collection := session.DB(exp.ToolOptions.Namespace.DB).C(exp.ToolOptions.Namespace.Collection)

	query := map[string]interface{}{}
	if exp.InputOpts != nil && exp.InputOpts.Query != "" {
		var err error
		query, err = getObjectFromArg(exp.InputOpts.Query)
		if err != nil {
			return nil, err
		}
	}

	q := collection.Find(query)

	if exp.InputOpts != nil && exp.InputOpts.Skip > 0 {
		q = q.Skip(exp.InputOpts.Skip)
	}
	if exp.InputOpts != nil && exp.InputOpts.Limit > 0 {
		q = q.Limit(exp.InputOpts.Limit)
	}

	if exp.InputOpts != nil && exp.InputOpts.Sort != "" {
		sortD, err := getSortFromArg(exp.InputOpts.Sort)
		if err != nil {
			return nil, err
		}
		sortFields, err := bsonutil.MakeSortString(sortD)
		if err != nil {
			return nil, err
		}
		q = q.Sort(sortFields...)
	}

	if len(query) == 0 && exp.InputOpts != nil && exp.InputOpts.ForceTableScan != true && exp.InputOpts.Sort == "" {
		q = q.Snapshot()
	}

	cursor := q.Iter()
	return &db.CursorDocSource{cursor, session}, nil
}