greedsin
greedsin

Reputation: 1272

maintain dialog data in firestore

I am looking for a way to store dialog data in Cloud Firestore. I am new to NoSQL Databases, so any tip is appreciated.

My goal is the following: I want a collection called "dialogs" which then stores the different dialogs in the following format (or similar)

dialogs:[
  {
    id:1,
    dialog: [
      {
        text: "hello is this a text?",
        turn: 0
      },
      {
        text: "yes this is a text",
        turn: 1
      }
    ],
  },
  {
    id: 2,
    dialog:[
      {
        text: "is this another text?",
        turn: 0
      },
      {
        text: "yes this is another text",
        turn: 1
      }
    ]
  }
]

My current approach in my web app looks like this:

db.collection("dialogs").add({id:1,dialog:[]})

First of all I would like to know if such a structure is possible (it does not exactly have to be this structure).

Second is it possible to generate a unique id and then query a particular id?

EDIT: I came up with this approach

const docRef = db.collection("dialogs").doc();
const dialog = {
  dialogId: docRef.id,
  intent: "SYMPTOM_INFORMATION",
  utterance: this.userInput,
  state: [],
  issuer: "USER",
  turn: 0
};
docRef.add(dialog)

What is now unclear for me is how I query all documents with the same dialogId

Upvotes: 0

Views: 53

Answers (1)

sllopis
sllopis

Reputation: 2368

Cloud Firestore allows you store multiple data types inside a document. You may want to check out the following documentation. This will depend on your use case and needs but, for example, you can have your database structure looking something like this:

let data = {
  numberExample: 1,
  objectExample: {
    stringExample: "is this another text?",
    numberExample: 1
  }
};

let setDoc = db.collection('dialogs').doc('SET_YOUR_DOCUMENT_ID').set(data);

Note that when you use set() to create a document, you must specify an ID for the document to create. On the other hand, to auto-generate an ID for a given document, you must use the add() method, instead, as suggested in the documentation.

Edit1:

To query all dialog documents inside a dialogs collection that meet the same id condition:

let dialogsRef = db.collection('dialogs');
let query = dialogsRef.where('id', '==', '1').get()
  .then(snapshot => {
    if (snapshot.empty) {
      console.log('No matching documents.');
      return;
    }  

    snapshot.forEach(doc => {
      console.log(doc.id, '=>', doc.data());
      // TODO: do something with the data.  
    });
  })
  .catch(err => {
    console.log('Error getting documents', err);
  });

To query all dialog documents and their id field inside a dialogs collection:

let dialogsRef = db.collection('dialogs');
let allDialogs = dialogsRef.get()
  .then(snapshot => {
    snapshot.forEach(doc => {
      console.log(doc.id, '=>', doc.data());
      // TODO: do something with the data.   
    });
  })
  .catch(err => {
    console.log('Error getting documents', err);
  });

More information can be found in the documentation.

Upvotes: 1

Related Questions