DocumentChange

DocumentChange

A DocumentChange represents a change to the documents matching a query. It contains the document affected and the type of change that occurred.

Constructor

new DocumentChange(type, document, oldIndex, newIndex)

Parameters:
Name Type Description
type string

'added' | 'removed' | 'modified'.

document QueryDocumentSnapshot

The document.

oldIndex number

The index in the documents array prior to this change.

newIndex number

The index in the documents array after this change.

Members

(readonly) doc :QueryDocumentSnapshot

The document affected by this change.

Example
let query = firestore.collection('col').where('foo', '==', 'bar');

let unsubscribe = query.onSnapshot(querySnapshot => {
  for (let change of querySnapshot.docChanges) {
    console.log(change.doc.data());
  }
});

// Remove this listener.
unsubscribe();

(readonly) newIndex :number

The index of the changed document in the result set immediately after this DocumentChange (i.e. supposing that all prior DocumentChange objects and the current DocumentChange object have been applied). Is -1 for 'removed' events.

Example
let query = firestore.collection('col').where('foo', '==', 'bar');
let docsArray = [];

let unsubscribe = query.onSnapshot(querySnapshot => {
  for (let change of querySnapshot.docChanges) {
    if (change.oldIndex !== -1) {
      docsArray.splice(change.oldIndex, 1);
    }
    if (change.newIndex !== -1) {
      docsArray.splice(change.newIndex, 0, change.doc);
    }
  }
});

// Remove this listener.
unsubscribe();

(readonly) oldIndex :number

The index of the changed document in the result set immediately prior to this DocumentChange (i.e. supposing that all prior DocumentChange objects have been applied). Is -1 for 'added' events.

Example
let query = firestore.collection('col').where('foo', '==', 'bar');
let docsArray = [];

let unsubscribe = query.onSnapshot(querySnapshot => {
  for (let change of querySnapshot.docChanges) {
    if (change.oldIndex !== -1) {
      docsArray.splice(change.oldIndex, 1);
    }
    if (change.newIndex !== -1) {
      docsArray.splice(change.newIndex, 0, change.doc);
    }
  }
});

// Remove this listener.
unsubscribe();

(readonly) type :string

The type of change ('added', 'modified', or 'removed').

Example
let query = firestore.collection('col').where('foo', '==', 'bar');
let docsArray = [];

let unsubscribe = query.onSnapshot(querySnapshot => {
  for (let change of querySnapshot.docChanges) {
    console.log(`Type of change is ${change.type}`);
  }
});

// Remove this listener.
unsubscribe();

Methods

isEqual(other)

Returns true if the data in this DocumentChange is equal to the provided value.

Parameters:
Name Type Description
other *

The value to compare against.