如何在mongoose / mongodb中获取对象数组中的最后一个对象?

时间:2015-06-03 12:37:14

标签: mongodb mongoose

以下是“对话”集合的文档结构。

n

我想从messages数组中获取最后一个对象,该数组在参与者数组中具有“looged-in user”id,并且登录用户必须出现在该对象的外部参与者数组中。

如果登录用户ID是“5530b6208ab0478c2bd23292”,那么我想要以下结果。

BufferedWriter out = new BufferedWriter(fstream); 
                            out.write("<kml>"); 
                            out.write("<Folder>");

                            out.write("<Placemark>");
                            out.write("<name>"+entry.getKey()+"</name>");
                            out.write("<LineString>");
                            out.write("<extrude>1</extrude>"); 
                            out.write("<tessellate>1</tessellate>");
                            out.write("<altitudeMode>clampToGround</altitudeMode>");
                            out.write("<coordinates>");
                            for(int i=0; i<entry.getValue().getCoordinates().size(); i++){ //path creation 
                                out.write(entry.getValue().getCoordinates().get(i).getLongitude()+","+
                                        entry.getValue().getCoordinates().get(i).getLatitude()+" ");
                            }
                            out.write("</coordinates>");
                            out.write("</LineString>");
                            out.write("</Placemark>");

                            for(int j=0; j<entry.getValue().getCoordinates().size(); j++){ //point creation 
                                out.write("<Placemark>");
                                out.write("<name>"+entry.getValue().getCoordinates().get(j).coordinates()+"</name>");
                                out.write("<tessellate>1</tessellate>");
                                out.write("<altitudeMode>clampToGround</altitudeMode>");
                                out.write("<Point>");
                                out.write("<coordinates>");
                                out.write(entry.getValue().getCoordinates().get(j).getLongitude()+","+
                                        entry.getValue().getCoordinates().get(j).getLatitude());
                                out.write("</coordinates>");
                                out.write("</Point>");
                                out.write("</Placemark>");
                            }

                            out.write("</Folder>");
                            out.write("</kml>");
                            out.close();
                        }

我需要为集合“对话”中的所有对象做同样的事情。我也希望填充“来自”字段&amp;参与者领域 我尝试了以下代码,但它无法正常工作。

{
        "_id" : ObjectId("5549ec1575bd899c17ec5163"),
        "messages" : [
                {
                        "subject" : "From fb to techniche",
                        "from" : ObjectId("5530b6208ab0478c2bd23292"),
                        "_id" : ObjectId("5549ec1575bd899c17ec5164"),
                        "created" : ISODate("2015-05-06T10:25:25.871Z"),
                        "read" : true,
                        "message" : "Message from fb to techniche",
                        "participants" : [
                                ObjectId("5530b6208ab0478c2bd23292")
                        ]
                },
                {
                        "subject" : "diohvoidso",
                        "from" : ObjectId("5534a588a12ae5ac11dc6f6b"),
                        "_id" : ObjectId("5549f42c75bd899c17ec5165"),
                        "created" : ISODate("2015-05-06T10:59:56.665Z"),
                        "read" : true,
                        "message" : "icdo ovdw iovhiwiohovhoihreiobv",
                        "participants" : [
                                ObjectId("5530b6208ab0478c2bd23292"),
                         ObjectId("5534a588a12ae5ac11dc6f6b")
                        ]
                },
                {
                        "subject" : "some reply subject",
                        "from" : ObjectId("5534a588a12ae5ac11dc6f6b"),
                        "_id" : ObjectId("5549f76275bd899c17ec5166"),
                        "created" : ISODate("2015-05-06T11:13:38.161Z"),
                        "read" : true,
                        "message" : "this is the body of the latest message",
                        "participants" : [
                                ObjectId("5534a588a12ae5ac11dc6f6b"),

                        ]
                }
        ],
        "participants" : [
                ObjectId("5530b6208ab0478c2bd23292"), // logged in user
                ObjectId("5534a588a12ae5ac11dc6f6b")
        ],
        "__v" : 10
}

请帮助我获得如下所需的结果。

{
        "_id" : ObjectId("5549ec1575bd899c17ec5163"),
        "messages" : [
                 {
                        "subject" : "diohvoidso",
                        "from" : ObjectId("5534a588a12ae5ac11dc6f6b"),
                        "_id" : ObjectId("5549f42c75bd899c17ec5165"),
                        "created" : ISODate("2015-05-06T10:59:56.665Z"),
                        "read" : true,
                        "message" : "icdo ovdw iovhiwiohovhoihreiobv",
                        "participants" : [
                                ObjectId("5530b6208ab0478c2bd23292"),
                         ObjectId("5534a588a12ae5ac11dc6f6b")
                        ]
                }
        ],
        "participants" : [
                ObjectId("5530b6208ab0478c2bd23292"), // logged in user
                ObjectId("5534a588a12ae5ac11dc6f6b")
        ],
        "__v" : 10
}

1 个答案:

答案 0 :(得分:1)

首先,您要检查participants数组是否有值,如果是,那么您想在req.user.idparticipants中搜索messages.participants,如果ID存在,则排序{{1带messages日期的数组,并选择最新的created

对于此检查,汇总查询:

message

或者如果您事先知道所有db.collectionName.aggregate({ "$match": { //check participants array not empty "participants": { "$not": { "$size": 0 } } } }, { "$unwind": "$messages" }, { "$sort": { // sort messages by created date so latest messages first come "messages.created": -1 } }, { "$match": { "messages.participants": ObjectId("5530b6208ab0478c2bd23292"), "participants": ObjectId("5530b6208ab0478c2bd23292") } }, { "$group": { "_id": "$_id", "messages": { // use $first to get latest message info "$first": "$messages" }, "participants": { "$first": "$participants" }, "__v": { "$first": "$__v" } } }).pretty() 数组,那么您应该尝试participants

elemMatch