maria
maria

Reputation: 159

Mongodb in array query wont get any result

Okay, so im getting all convensations with the following query:

return convensations.findOne({}).lean().then(function (convensations) {
console.log(convensations);
});

that would results in the following:

{ _id: 598dd4458b08b727dc53d4a6,
  initiateduser: 'spillinfo',
  gang: false,
  global: false,
  members: [ 'spillinfo', '59312d2b329b7535b07e273c' ],
  name: 'no name',
  __v: 0 }

thats totally fine, but when i do

var userid = "59312d2b329b7535b07e273c";
      return convensations.find({members: userid}).lean().then(function (convensations) {
console.log(convensations);
});

it wont get me any results, why is that?

what im i doing wrong to check if the userid is within the members array?

UPDATE EDIT:

DB schema:

new Schema({
    initiateduser : String,
    name: {type:String, default: 'no name'},
    members: { type: Array, default: []},
    time: Number,
    global: { type: Boolean, default: false},
    gang: { type: Boolean, default: false},

});

and example of inserting / creating new:

   var conv = new convensations();
            conv.members = [userid, user2];
            conv.initiateduser = userid;
            conv.save(function (err,room) {

edit2:

some debug from query:

getting convensations with userid 59312d2b329b7535b07e273c
Mongoose: convensations.find({ members: ObjectId("59312d2b329b7535b07e273c") }, { fields: {} })
convensations: 0

Upvotes: 0

Views: 29

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222582

Change your schema as,

new Schema({
    initiateduser : String,
    name: {type:String, default: 'no name'},
    members: { type: [String], default: []},
    time: Number,
    global: { type: Boolean, default: false},
    gang: { type: Boolean, default: false},

});

Upvotes: 1

Related Questions