我需要能够在包含我所有聊天消息且带有“ updated_at”时间戳的数组上运行查询。我不想检索所有结果并将它们加载到内存中,因为随着时间的推移这将是巨大的负载。我想访问该数组,按升序对其进行排序,然后得到第一个块10。
$ chatroomConversation = DB :: connection('mongodb')-> collection($ chatroomCollection) -> orderBy('messages.updated_at','asc');
我当然只能得到一个结果,即父文档。
答案 0 :(得分:1)
这就是我所做的:
代码动态创建集合:
public function newCollection ($collectionName) {
if (config('settings.DebugTraceLevelFunctions') == true) { Log::channel('merlin_debug')->info('newCollection function in CreateCustomCollections.php'); }
Schema::connection('mongodb')->create($collectionName, function ($collection) {
$collection->string('UID');
});
}
如何使用此类:
$chatroomCollectionName = 'chatroom_'.$newChatroomData['UID'];
$newCustomCollection = new CreateCustomCollections();
$newCustomCollection->newCollection($chatroomCollectionName);
这是您检索数据的方式:
//generate the collection name to retrieve it
$chatroomCollection = 'chatroom_' . $chatroomUID;
//connect to the database and insert the new message
$chatroomConversation = DB::connection('mongodb')
->collection($chatroomCollection)
->insert([
'UID' => (string)$varUID,
'member' => $newMessage['member'],
'message' => $newMessage['message'],
'status' => $newMessage['status'],
'created_at' => $timeStamp,
'updated_at' => $timeStamp,
]);