0

I am trying to find and update a subdoc using mongoose and mongdb but I can't seem to find the subdocs using the ids.

My schema

var Attending = new Schema({
   users: []
});

var Query = new Schema({
   name: [],
   url: [],
   attending: [Attending],
   img: []
});

Using the ids for both docs, just finding returns null for the doc. However, if i only using the general Id I can find the entire doc but not the subdoc.

 Queries.findOne(
     { "_id": queryId, "attending._id": attendingId },
      function(err, doc) {
        if (err) throw err;
        console.log(doc);
        res.render('index');
     }
 );

sample doc

{
"_id": {
    "$oid": "58d995026d7c8f0a3028a50f"
},
"img": [
    "https://pic1",
    "https://pic2"
],
"attending": [
    {
        "_id": {
            "$oid": "58d995026d7c8f0a3028a505"
        },
        "users": [
            "somebody"
        ]
    },
    {
        "_id": {
            "$oid": "58d995026d7c8f0a3028a506"
        },
        "users": [
            "another person"
        ]
    }
],
"url": [
    "https://www.yelp.com/2",
    "https://www.yelp.com/1"

],
"name": [
    "The Rivermill",
    "Sharkeys"
],
"__v": 0
}

I am using the correct ids and expect to the first subdoc in attending to be returned or whichever one is searched. Instead null is returned.

Queries is made like this:

var names = [];
var urls = [];
var imgUrls = [];
var attendingArray = [];

for (var i=0; i<10; i++) {
    names.push(businesses[i].name);
    urls.push(businesses[i].url);
    imgUrls.push(businesses[i].image_url);

    //add attending schema
    var newAttending = Attending({
          users: ["somebody"]
    });

   attendingArray.push(newAttending);
} 

var newQuery = Queries({
        name: names,
        url: urls,
        attending: attendingArray,
        img: imgUrls
});    
andrewgi
  • 620
  • 1
  • 7
  • 22
  • 1
    When you separate by comma, you are using an AND operator, not an OR. – Israel Zinc Mar 28 '17 at 00:57
  • I want to use the AND operator though. I am looking first for the query with the id and then within that query I want to find the attending subdoc with that id. This is demonstrated in this example: http://stackoverflow.com/questions/26156687/mongoose-find-update-subdocument – andrewgi Mar 28 '17 at 01:57
  • Have you verified the input values ? Can you add a sample doc that you are expecting your search to return and your expected response ? Do you have separate collections or `Attending` documents is embedded in `Queries` collection ? – s7vr Mar 28 '17 at 02:24
  • sample doc added. The attending docs are embedded in the queries collection as shown. – andrewgi Mar 28 '17 at 02:37
  • I just ran your code with your data. It is giving me the data back. `var Queries = mongoose.model('collectionname', Query); Queries.findOne( { "_id": "58d995026d7c8f0a3028a50f", "attending._id": "58d995026d7c8f0a3028a505" }, function(err, doc) { if (err) throw err; console.log(doc); } );` – s7vr Mar 28 '17 at 02:47
  • I just ran the same thing and I get null! The only difference is that I didn't include `var Queries = mongoose.model('collectionname', Query);` but Im connecting to the database through mlab and if I remove the `attending._id` I get the full doc. – andrewgi Mar 28 '17 at 03:05
  • How is `Queries ` model created ? – s7vr Mar 28 '17 at 15:10
  • I added query construction. Let's discuss in chat. – andrewgi Mar 28 '17 at 16:06

0 Answers0