简体   繁体   English

猫鼬,获取元素在数组中的位置

[英]Mongoose, get position of element in array

I am developping the messaging part of an app. 我正在开发应用程序的消息传递部分。

I have a schema that represents a conversation. 我有一个代表对话的schema It contains an array of ObjectId , and each ObjectId refers to a message. 它包含一个ObjectId数组,每个ObjectId引用一条消息。

I would like the user to sent to my API a message _id , so I can retrieve all the messages _id s on the array, after the one he sent. 我想用户发送到我的API的消息_id ,这样我就可以检索所有消息_id阵列上的年代,他送出一 So he can update with all the messages he doesn't have yet. 这样他就可以更新所有他还没有的消息。

I can I get the position of the _id he sends me, within the messages: [ObjectId] array, into the query ? 我可以在messages: [ObjectId]数组中获取他发送给我的_id的位置到查询中吗?

Mongo doesn't support this type of query on the server, so if you take this approach you will have to do it within your application. Mongo在服务器上不支持这种类型的查询,因此,如果采用这种方法,则必须在应用程序中进行。

You might want to consider other possible architectures. 您可能需要考虑其他可能的体系结构。 First, after you have the position of the last ObjectId, how will you make your subsequent query? 首先,找到最后一个ObjectId的位置后,如何进行后续查询? You could instead query the messages collection directly, using the timestamp that is encoded in the _id to return only newer messages: 您可以改为使用_id中编码的时间戳直接查询邮件集合,以仅返回更新的邮件:

db.messages.find({_id: {$gt: lastViewedMessage._id}})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM