6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Mongo Shellの--evalオプションでオブジェクトをJSON出力する

Last updated at Posted at 2015-12-16

この記事はMongoDB Advent Calendar 2015 - Qiitaの16日目です。

mongoコマンドで起動できるMongo Shellには--evalオプションがあり、対話式のMongo Shellを開かずに直接コマンドを評価・実行することができます。

しかし、この機能で普通にオブジェクトを出力しようとすると、対話式モードと違い以下のような出力になってしまいます。

$ mongo --eval 'rs.conf()'
MongoDB shell version: 3.0.6
connecting to: oreno-mongodb00-00:27017/test
[object Object]

中身が見えない!!

そこでprintjson()です。これを使うと--evalであってもオブジェクトの中身まで表示できます。

$ mongo --eval 'printjson(rs.conf())'
MongoDB shell version: 3.0.6
connecting to: oreno-mongodb00-00:27017/test
{
	"_id" : "oreno-mongodb00",
	"version" : 11,
	"members" : [
		{
			"_id" : 0,
...

やったね。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?