ユーザ用ツール

サイト用ツール


mongodb:style_script

整形スクリプト

dot.mongorc.js

標準関数printjson()をp()で実現

DBQuery.prototype.p = function() {
	var args = arguments
	this.forEach(function(e) {
		var result = {}
		if(args.length == 0) {
			result = e
		} else {
			for(var i = 0; i < args.length; i++) {
				var target = e
				var keys = args[i].split('.')
				var flag = true
				keys.forEach(function(key) {
					if(key in target) {
						target = target[key]
					} else {
						flag = false
						return
					}
				})
				if(!flag) {
					target = '(undefined)'
				}
				if(args.length == 1) {
					result = target
				} else {
					result[[args[i]]] = target
				}
			}
		}
		printjson(result)
	})
}
# vi /data/dot.mongorc.js
~
# mongo /data/dot.mongorc.js --shell
> db.testdata.find({entryId: {$lt: 3}}).p()
{
       "_id" : ObjectId("5109be7579ee8df58e8feca6"),
       "entryId" : 1,
       "createdDatetime" : ISODate("2013-01-31T00:44:37.080Z"),
       "name" : "test01"
}
{
       "_id" : ObjectId("5109be7579ee8df58e8feca7"),
       "entryId" : 2,
       "createdDatetime" : ISODate("2013-01-31T00:44:37.080Z"),
       "name" : "test02"
}

Key指定も可能

> db.testdata.find({entryId: {$lt: 3}}).p('entryId')
1
2
>
mongodb/style_script.txt · 最終更新: 2025/02/16 13:53 by 127.0.0.1