0

Absolute mongodb newbie here, and I must say I'm not impressed by the documentation. Anyway, my problem is I have a database on Mongodb Atlas, a Stitch application with a next.js frontend and trying to retrieve data like this:

import { setStitchClient } from '../utils/stitch';

export default class MyPage extends React.Component {
  static async getInitialProps() {
    const stitchClient = await setStitchClient();
    const db = stitchClient.service('mongodb', 'mongodb-atlas').db('dbName');

    await stitchClient.login();
    const docs = await db
      .collection('test')
      .find()
      .limit(200)
      .execute();
    return { docs: docs };
}

  render() {...}

The test collection includes 150 documents imported with mongoimport cli. I verified the documents are correctly imported. However the .find() above finds no records and the result is an empty array. If I create a document through the frontend in another collection, I can find it without problems.

I guess there is no simple answer here but any hint would be highly appreciated. Thanks a lot!

proko
  • 175
  • 2
  • 11
  • What is your question? Is something not working? If so, what? – Ann L. Jun 16 '18 at 22:42
  • Sorry, I completely omitted the actual question. Updated now. So the question is: Do you see anything wrong in my code that could explain why I'm finding no documents in an imported collection? – proko Jun 17 '18 at 06:42
  • Not sure about what is exactly stitch but I think your problem might be one out of two possible. 1. Sometimes the mongo abstractions for languages are very picky about their queries, which means that sometimes you have to send an empty json if you want to actually retrieve everything like find({}) 2. I read that stitch is asynchronous? Not sure about this, but if so, you might be having some issue like this. You might try adding a .then clause after the execute method and work within its callback. Hope this gives you at least an idea. – Roger Jun 17 '18 at 08:13
  • I like @Roger's idea. I also think you might want to take out the `limit` call, since you only have 150 documents and this is test code. (Remove one "black box" and you have one less thing to wonder about the internals of.) Finally, while I don't know Stitch at all, can you simplify this code and how it is executed? Perhaps make it synchronous, thus ensuring timing isn't an issue? – Ann L. Jun 17 '18 at 14:42

2 Answers2

0
  1. Can you run a find or findOne from the CLI with positive results? Or confirm in Compass that the collection is there, in that db...
  2. Been a while since I did any mongo stuff, but I think you need to add the empty object for the query so

       .find({}) or .findOne({})
    
  3. Consider going with express + mongoose instead of stitch while you are learning to work with mongo. It's the tried and tested setup for a backend using mongo. It will be easier to refactor your code to stitch later on, than to move forward while getting stuck on lacking documentation.

Hope that helps!

  • Thanks for your answer @stoepszli. I tried 1 and 2. Can see the documents in Compass and empty object didn't change anything. I'll have a look at mongoose, thanks for the suggestion! – proko Jun 17 '18 at 08:17
0

first of all, you should import "mongodb" in your model.

if you want use find() then in your method do this :

db.collection("your collection name")
.find({ _id: mongodb.ObjectID(prodId) })
.next()
.then(product => {
        console.log(product);
        return product;
      })

the "mongodb.ObjectID(id) let you to compare the id and _id in your DB by string.

the next() is so important to process continue

but if you want use findOne({}) do this :

db.collection("products")
    .findOne({ _id: mongodb.ObjectID(prodId) })
    .then(product => {
            console.log(product);
            return product;
          })
S.Saderi
  • 4,755
  • 3
  • 21
  • 23