Skip to content

Commit 0d8351c

Browse files
author
Will Banfield
committed
MGO-141 implement watch function
1 parent 7940d20 commit 0d8351c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

changestreams.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,37 @@ type ChangeStreamOptions struct {
3939
Collation *Collation
4040
}
4141

42+
// Watch constructs a new ChangeStream capable of receiving continuing data
43+
// from the database.
44+
func (coll *Collection) Watch(pipeline interface{},
45+
options ChangeStreamOptions) (*ChangeStream, error) {
46+
47+
if pipeline == nil {
48+
pipeline = []bson.M{}
49+
}
50+
51+
pipe := constructChangeStreamPipeline(pipeline, options)
52+
53+
pIter := coll.Pipe(&pipe).Iter()
54+
55+
// check that there was no issue creating the iterator.
56+
// this will fail immediately with an error from the server if running against
57+
// a standalone.
58+
if err := pIter.Err(); err != nil {
59+
return nil, err
60+
}
61+
62+
pIter.isChangeStream = true
63+
64+
return &ChangeStream{
65+
iter: pIter,
66+
collection: coll,
67+
resumeToken: nil,
68+
options: options,
69+
pipeline: pipeline,
70+
}, nil
71+
}
72+
4273
// Next retrieves the next document from the change stream, blocking if necessary.
4374
// Next returns true if a document was successfully unmarshalled into result,
4475
// and false if an error occured. When Next returns false, the Err method should

0 commit comments

Comments
 (0)