The mongodb query collection starting with _

In principle, the question is simple:

How can I execute a query on a collection that starts with _?

For example, if I have 2 collections test and _test , and I try db.test.findOne() and db._test.findOne() in mongoshell , the first one works as intended, and the second tells me TypeError: db._testhas no properties (shell):1

+4
source share
1 answer

Put it in quotation marks and use the getCollection method. See Article.

Example To create the _foo collection and insert the document {a: 1}, use the following operation:

 db.getCollection("_foo").insert( { a : 1 } ) 

To execute the query, use the find () method as follows:

 db.getCollection("_foo").find() 
+4
source

Source: https://habr.com/ru/post/1493874/


All Articles