mongodb-shell

How do you do an AND query on an array in mongodb?

核能气质少年 提交于 2020-01-03 17:02:03
问题 I have an array with tags which is part of a document, eg ["red", "green", "blue", "white", "black"] Now I want to find all documents, which have red AND blue. 回答1: Use the $all condition to find records that match both "red" and "blue" conditions. db.my_collection.find({tags: { $all : ["red","blue"]}}) If you want records that match either "red" or "blue" then you can use the $in condition. db.my_collection.find({tags: { $in : ["red","blue"]}}) 回答2: Also in case someone is wondering how to

Mongodb: Combine $in and $nin

情到浓时终转凉″ 提交于 2019-12-12 04:25:15
问题 In my collection posts I've documents like this [{ _id : ObjectId("post-object-id"), title : 'Post #1', category_id : ObjectId("category-object-id") }] I need to make some queries where I those a range of posts based on their category_id (can be multiple ids) but exclude some of them. I've tried with the query (in shell): db.posts.find({$and: [ {_id: { $nin : ['ObjectId("post-object-id")']}}, {category_id : { $in : ['ObjectId("category-object-id")']}} ]}) I returns 0 if count(). However, if I

In MongoShell: Not able to connect to my collection, db.collection_name return NaN

旧巷老猫 提交于 2019-12-11 00:05:50
问题 I am using MongoDB Enterprise, MongoDB shell version: 3.2.5 I have a db = mydb and a collections = ['events', 'events__2015-12-01', 'events__2015-11-01'] I have a python/pymongo script where I can connect to every document but in mongo shell I cannot to connect to the dated collections? in other words mongodb> use mydb switched to db mydb mongodb> db.event mydb/event mongodb> db.event__2015-12-01 NaN mongodb> db.event__2015-11-01 NaN mongodb> show collections event event__2015-11-01 event_

How to find if Mongodb is running in auth mode in shell script?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 19:52:52
问题 I am having a mongodb instance running which is running auth mode in my server machine. Currently I am using a shell scipt to get whether there is a mongodb instance running or not. How Can I check whether if the mongodb is running in a auth mode or non auth mode . 回答1: If you just want to test whether you can connect to a MongoDB server without authentication via bash , you can use a script similar to the following: #!/bin/bash # Connect to MongoDB address (host:port/dbname) specified as

Mongodb: Perform a Date range query from the ObjectId in the mongo shell

不想你离开。 提交于 2019-11-27 19:26:41
I have a collection that looks like this: { _id: ObjectId("50a68673476427844b000001"), other fields } I want to do a range query to find records between two dates. I know that I can get the date from the ObjectId in the mongo shell var doing this: var aDate = ObjectId().getTimestamp() but there isn't a way (as far as I can figure out at the moment) to create an ObjectId consisting of just the timestamp portion - I think my ideal solution is non-functioning mongo shell code would be: var minDate = ObjectId(new Date("2012-11-10")); var maxDate = ObjectId(new Date("2012-11-17")); Use the find

MongoDB $or query

会有一股神秘感。 提交于 2019-11-27 02:03:57
I run following query in mongo shell: db.Profiles.find ( { $or: [ {"name": "gary"}, {"name": "rob"} ] } ) It just returns nothing as expected(JSON)? Use $in For the query in the question, it's more appropriate to use $in db.Profiles.find ( { "name" : { $in: ["gary", "rob"] } } ); Why doesn't it work There's a missing quote - the cli is waiting for you to finish the second part of your or: db.Profiles.find ( { $or : [ { "name" : "gary" }, {"name":"rob} ] } ) ..............................................................^ You need to finish the query sufficiently for the cli to parse it for it

Mongodb: Perform a Date range query from the ObjectId in the mongo shell

孤街浪徒 提交于 2019-11-26 22:46:20
问题 I have a collection that looks like this: { _id: ObjectId("50a68673476427844b000001"), other fields } I want to do a range query to find records between two dates. I know that I can get the date from the ObjectId in the mongo shell var doing this: var aDate = ObjectId().getTimestamp() but there isn't a way (as far as I can figure out at the moment) to create an ObjectId consisting of just the timestamp portion - I think my ideal solution is non-functioning mongo shell code would be: var

MongoDB $or query

纵饮孤独 提交于 2019-11-26 09:53:32
问题 I run following query in mongo shell: db.Profiles.find ( { $or: [ {\"name\": \"gary\"}, {\"name\": \"rob\"} ] } ) It just returns nothing as expected(JSON)? 回答1: Use $in For the query in the question, it's more appropriate to use $in db.Profiles.find ( { "name" : { $in: ["gary", "rob"] } } ); Why doesn't it work There's a missing quote - the cli is waiting for you to finish the second part of your or: db.Profiles.find ( { $or : [ { "name" : "gary" }, {"name":"rob} ] } ) ......................