猫鼬查询数组,带有id数组

时间:2019-09-13 10:06:42

标签: node.js mongodb mongoose

我需要查询数组中的条目,文档如下所示:

{
   "_id":"5d7b4ef6f691b71b5097e9cb",
   "name":"1568362230828",
   "commands":[
      {
         "_id":"5d7b4ef6f691b71b5097e9d1",
         "name":"Command - 0"
      },
      {
         "_id":"5d7b4ef6f691b71b5097e9d0",
         "name":"Command - 1"
      },
      {
         "_id":"5d7b4ef6f691b71b5097e9cf",
         "name":"Command - 2"
      },
      {
         "_id":"5d7b4ef6f691b71b5097e9ce",
         "name":"Command - 3"
      },
      {
         "_id":"5d7b4ef6f691b71b5097e9cd",
         "name":"Command - 4"
      },
      {
         "_id":"5d7b4ef6f691b71b5097e9cc",
         "name":"Command - 5"
      }
   ],
   "__v":0
}

现在我想通过该ID获取所有命令:

model.find({
commands: {
_id: ["5d7b4ef6f691b71b5097e9cf", "5d7b4ef6f691b71b5097e9cf"] }})

伪查询,这不起作用!

查询如何显示?!

const schema = new mongoose.Schema({
    name: String,
    commands: [{
        name: String
    }]
});

const model = mongoose.model('Endpoints', schema);

2 个答案:

答案 0 :(得分:1)

如果要取消过滤仅注释文档中的注释,请使用此查询

 var results = new ComposeExtensionResult()
                {
                    AttachmentLayout = "list",
                    Type = "result",
                    Attachments = new List<ComposeExtensionAttachment>(),
                };

   var card = CardHelper.CreateCardForExperties(pos, true);
   var composeExtensionAttachment = card.ToAttachment().ToComposeExtensionAttachment();
results.Attachments.Add(new ComposeExtensionAttachment
                        {
                            ContentType = "application/vnd.microsoft.teams.card.adaptive",
                            Content = JsonConvert.DeserializeObject(updatedJsonString),
                            Preview = composeExtensionAttachment
                        });

如果您要过滤评论中的评论

SELECT 
r.CourseName,
r.CourseID,
r.SubjectName,
r.SubjectColour,
r.RoomName,
TimetablePeriods.PeriodName
FROM
    (SELECT
    Course.CourseName,
    Course.CoureID,
    Subject.SubjectName,
    Subject.Colour,
    Room.RoomName,
    Timetable.Period_ID
    FROM
    Course,
    Timetable,
    Subject,
    CourseMembership,
    Room
    WHERE
    Course.CourseID = Timetable.Course_ID AND
    Course.Subject_ID = Subject.SubjectID AND
    Timetable.Room_ID = Room.Room_ID AND
    CourseMembership.Course_ID = Course.CourseID AND
    CourseMembership.Student_ID = 123) r
RIGHT JOIN TimetablePeriods ON
TimetablePeriods.PeriodID = r.Period_ID
ORDER BY TimetablePeriods.SortOrder ASC

或不聚合

Endpoints.find({
   commands: {
     $elemMatch: {
       _id: { $in: ["5d7b4ef6f691b71b5097e9cf", "5d7b4ef6f691b71b5097e9cf"] }
     }
   }
});

答案 1 :(得分:-1)

您可以尝试一下。

model.find({
    '_id': { $in: [
        mongoose.Types.ObjectId('5d7b4ef6f691b71b5097e9cfc'),
        mongoose.Types.ObjectId('5d7b4ef6f691b71b5097e9cfd')
    ]}
}, function(err, docs){
     console.log(docs);
});