Mongodb聚合查找合并集合的方法是什么?

huangapple go评论76阅读模式
英文:

Mongodb aggregate lookup for merged collections?

问题

合并查询会花费更多的时间,例如从用户集合中获取用户名查询和从产品帖子中获取查询,会导致服务器处理更多的请求,从用户端发送更多的请求。

我的问题是,最新的golang mongodb集成是否可以使用聚合函数?

英文:

Merged queries takes more time for requests, for instance having a username query from user collection and having a query from productpost, costs server process power with more requests sent from userend.

My Q is can we have aggregate function for the latest golang mongodb integration?

答案1

得分: 0

经过很长时间的努力,最简单的方法是通过请求而不是查找来完成,因为这将需要遍历所有文档来查找:

以下是mongodb文档结构:

{"_id":{"$oid":"6198b5cf985bb6b616480463"},"userid":{"$oid":"613dfcca9e7e2014c12dfeef"},"attach":"[\"https://image.bitfinicon.com/be/images/2021/10/OTKAR2021-10-0222-40-33_5877034727566088.png\"]","title":"我是标题"}

然后在JS中调用请求(使用Sveltekit新一代):

<div class="author-name">
    <!-- DONEAPPLYCHANGES DONE ALDOLAP -->
    {#if config.name === 'aldolap'}
        <div class="author-name-prefix">{product.price} TL</div>
    {/if}
        {#await getuserhere2(product.userid)}
            <p>...等待中</p>
        {:then number}
        <a href={`/u/` + number.username + `_` + `${product.userid}`} >{number.username}</a>
            
        {:catch error}
            <p style="color: red">{error.message}</p>
        {/await}
</div>
英文:

After much time spent on this. The easiest way to do this is via requests than lookups since it will require to go through all documents to find:

Here is mongodb document structure

{&quot;_id&quot;:{&quot;$oid&quot;:&quot;6198b5cf985bb6b616480463&quot;},&quot;userid&quot;:{&quot;$oid&quot;:&quot;613dfcca9e7e2014c12dfeef&quot;},&quot;attach&quot;:&quot;[\&quot;https://image.bitfinicon.com/be/images/2021/10/OTKAR2021-10-0222-40-33_5877034727566088.png\&quot;]&quot;,&quot;title&quot;:&quot;I&#39;m title here&quot;}

Then in the JS you call requests (Using Sveltekit new generation):

&lt;div class=&quot;author-name&quot;&gt;
			&lt;!-- DONEAPPLYCHANGES DONE ALDOLAP --&gt;
			{#if config.name === &#39;aldolap&#39;}
				&lt;div class=&quot;author-name-prefix&quot;&gt;{product.price} TL&lt;/div&gt;
			{/if}
				{#await getuserhere2(product.userid)}
					&lt;p&gt;...waiting&lt;/p&gt;
				{:then number}
				&lt;a href={`/u/` + number.username + `_` + `${product.userid}`} &gt;{number.username}&lt;/a&gt;
					
				{:catch error}
					&lt;p style=&quot;color: red&quot;&gt;{error.message}&lt;/p&gt;
				{/await}
		&lt;/div&gt;

huangapple
  • 本文由 发表于 2021年8月31日 12:00:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/68992707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定