<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MongoDB &#8211; Ling&#039;s Note</title>
	<atom:link href="https://www.chunho-ling.com/category/computing/database/mongodb/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.chunho-ling.com</link>
	<description>Everything related IT, and me.</description>
	<lastBuildDate>Tue, 08 Jan 2019 04:57:43 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>
<site xmlns="com-wordpress:feed-additions:1">104401516</site>	<item>
		<title>[MongoDB] 顯示collection 內的column</title>
		<link>https://www.chunho-ling.com/mongodb-%e9%a1%af%e7%a4%bacollection-%e5%85%a7%e7%9a%84column/</link>
					<comments>https://www.chunho-ling.com/mongodb-%e9%a1%af%e7%a4%bacollection-%e5%85%a7%e7%9a%84column/#respond</comments>
		
		<dc:creator><![CDATA[C.H. Ling]]></dc:creator>
		<pubDate>Tue, 08 Jan 2019 04:57:43 +0000</pubDate>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[MongoDB]]></category>
		<guid isPermaLink="false">https://www.chunho-ling.com/?p=1187</guid>

					<description><![CDATA[利用MongoDB 在編程上帶來不少方便, 但稍一不慎就會令到結構變得難以複雜. 示範中會利用mongoDB 讀取collection 中的property name. 加入Function isArray() 及m_sub() 作檢查Array 及recursive load. isArray = function (v) { return v &#38;&#38; typeof v === 'object' &#38;&#38; typeof v.length === 'number' &#38;&#38; !(v.propertyIsEnumerable('length')); } m_sub = <a class="mh-excerpt-more" href="https://www.chunho-ling.com/mongodb-%e9%a1%af%e7%a4%bacollection-%e5%85%a7%e7%9a%84column/" title="[MongoDB] 顯示collection 內的column">[...]</a>]]></description>
										<content:encoded><![CDATA[<p>利用MongoDB 在編程上帶來不少方便, 但稍一不慎就會令到結構變得難以複雜. 示範中會利用mongoDB 讀取collection 中的property name.<span id="more-1187"></span></p>
<ol>
<li>加入Function isArray() 及m_sub() 作檢查Array 及recursive load.
<pre class="EnlighterJSRAW" data-enlighter-language="null">isArray = function (v) {
  return v &amp;&amp; typeof v === 'object' &amp;&amp; typeof v.length === 'number' &amp;&amp; !(v.propertyIsEnumerable('length'));
}

m_sub = function(base, value){
  for(var key in value) {
    if(key != "_id") { 
        emit(base + "." + key, null);
    if( isArray(value[key]) || typeof value[key] == 'object'){
      m_sub(base + "." + key, value[key]);
    }
}
  }
}

db.system.js.save( { _id : "isArray", value : isArray } );
db.system.js.save( { _id : "m_sub", value : m_sub } );
</pre>
</li>
<li>&nbsp;設定map reduce 以決定存取資料及filter 設定.
<pre class="EnlighterJSRAW" data-enlighter-language="null">map = function(){
  for(var key in this) {
    emit(key, null);
    if( isArray(this[key]) || typeof this[key] == 'object'){
      m_sub(key, this[key]);
    }
  }
}

reduce = function(key, stuff){ return null; }</pre>
</li>
<li>存取collection.
<pre class="EnlighterJSRAW" data-enlighter-language="null">var collectionName="&lt;&lt;Collection Name&gt;&gt;";
var mr = db.runCommand({
    "mapreduce" : collectionName, 
    "map" : map, 
    "reduce" : reduce,
    "out": "things" + "_keys"});
var result=db[mr.result].distinct("_id");
print(result);</pre>
<p>&nbsp;</li>
</ol>
]]></content:encoded>
					
					<wfw:commentRss>https://www.chunho-ling.com/mongodb-%e9%a1%af%e7%a4%bacollection-%e5%85%a7%e7%9a%84column/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1187</post-id>	</item>
	</channel>
</rss>
