MongoDB writing a correct query - mongodb

I am new to MongoDB and Im trying to write a query to extract data from a collection I created but I am stuck in trying to do so. Any help would be greatly appreciated.
I want to get all the fields except _id for all movies that are NOT based on things
How could I do this in a MongoDB query? I've read and looked up many things trying to learn about Mongo but so far I haven't gotten anything. Thank you very much in advance.
This is my collection:
{ "_id" : ObjectId("5363738hhhe2282828282w"), "coll" : "PageMaster" }
{ "_id" : ObjectId("0000222211223333sssswq2"), "coll" : "Honey1", "Jink" : { "head" : "Jink1"} }
{ "_id" : ObjectId("5hjkwwowwj7373365252wwww"), "coll" : "Rodger", "things" : { "head": "Honey"} }

Just check for the records where the key BOOK does not exist. Those are the records that we want. And exclude the _id field in the projection parameter.
db.MOVI.find({"BOOK":{$exists:false}},{"_id":0})
EDIT:
For using printjson to print the cursor contents:
var myCursor = db.MOVI.find({"BOOK":{$exists:false}},{"_id":0});
myCursor.forEach(function(doc)
{
printjson(doc);
})

Related

Using Mongodb _id field to query partially on one or more of the composite fields

I am using _id field as a compound key for my document with 2 fields as below.
{
"_id" : {
"timestamp" : ISODate("2016-08-25T05:43:00.000-19:30"),
"hostName" : "nj"
}
}
What I noticed it I am able to only query if I use both the fields together in my query. If I use one of them, I do not get any documents returned.
db.getCollection('sales').find(
{
"_id" : {
"hostName" : "tryme"
}
}
db.getCollection('sales').find(
{
"_id" : {
"timestamp" : ISODate("2016-08-25T05:43:00.000-19:30")
}
}
The above script does not return any documents.
Also, I am not able to use $gte/$lte operators on the date fields,
db.getCollection('sales').find(
{
"_id" : {
"timestamp" : {
"$lte":ISODate("2016-08-25T04:51:00.000-19:30")
},
"hostName" : "tryme"
}
}
)
The above also does not return any docs.
The below queries works but I see as per explain() it uses a collection scan and index is not used.
db.getCollection('sales').find(
{
"_id.timestamp" : ISODate("2016-08-25T04:51:00.000-19:30"),
"_id.hostName" : "tryme"
}
)
==
db.getCollection('sales').find(
{
"_id.timestamp" : {
"$gte": ISODate("2016-08-25T04:52:00.000-19:30")
},
"_id.hostName" : "tryme"
}
)
Not sure If I have understood how the _id field works correctly.
Basically, I want to be able to use partial fields of the composite query and also use the date type field also for range queries like between/greaterthan/lesser than etc at the same time leveraging the index on the _id field.
Can someone please help me on this.
Thanks,
Sri
From the docs:
MongoDB uses the dot notation to access the elements of an array and
to access the fields of an embedded document.
Your firsts attempts doesn't work because you are passing a nested object as query which matches for equality, use dot notation instead.

Getting MongoDB error - SyntaxError: Unexpected token?

I am a Mongo database. There is a collection called _migrations. I am trying to insert couple of documents into the collection. But I am getting the following error SyntaxError: Unexpected token ?. My code is below.
db._migrations.insert([
{
"_id" : "1_create-organization-collection"
},
{
"_id" : "2_create-dataFilter-collection"
},
{
"_id" : "3_create-application-collection"
},
{
"_id" : "4_migrate-datafilters-to-mongo"
},
{
"_id" : "5_Add-Salesforce-DataFilters"
},
{
"_id" : "6_biq-repository-data-fiter"
}]);
What am I doing wrong?
Can you please trying removing underscore("") in your collection name? I tried with underscore but it did not work for me, but when I tried without underscore("") same data got inserted. So, please try without underscore in your collection name.
_id field value can be inserted after type case with ObjectId. it will work like below -
db._migrations.insert([
{ "_id" : ObjectId("1_create-organization-collection") },
{ "_id" : ObjectId("2_create-dataFilter-collection") }
]);
Please refer below links for more details about the issue:
Is there a convention to name collection in MongoDB?
Mongo client can't access collections prefixed with an underscore
Mongo shell does not support collection name starting with underscore("_")

MongoDB mass update inserting data from one document to another one

I'm still learning about MongoDB and I would like to know if someone could help me with the situation I'm facing.
I'm taking over a DB structure that has been created like a relational DB abd I would like to embed a full document (instead of a reference only to the document) into all my documents.
Let me try to explain the best I can:
I have an activity table that references a user using its userID
activity : {
"user_id" : ObjectId("5324a18d3061650002030000")
}
user_id is the primary id of another document called user.
user:
{
"_id" : ObjectId("5324a18d3061650002030000"),
"active" : true,
"birth_date" : ISODate("1980-03-25T00:00:00.000Z")
}
What I would like to do is to insert my user into my activity document:
activity : {
"user_id" : ObjectId("5324a18d3061650002030000")
user:
{
"_id" : ObjectId("5324a18d3061650002030000"),
"active" : true,
"birth_date" : ISODate("1980-03-25T00:00:00.000Z")
}
}
I would like to do that for all my activity documents (knowing that they all reference different users of course), what would be the best way to do that please?
Thanks a lot guys !!!
It should be simpler like this:
db.users.find().forEach(function(doc) {
db.activity.update({user_id: doc._id}, {$set:{user:doc}});
});

mongo db update subdocument - subdocument not in array

I have a document like this,
{
"S" : {
"500209" : {
"total_income" : 38982,
"interest_income" : 1714,
"reported_eps" : 158.76,
"year" : 201303
}
},
"_id" : "pl"
}
I am trying to update this document like this,
{
"S" : {
"500209" : {
"total_income" : 38982,
"interest_income" : 1714,
"reported_eps" : 158.76,
"year" : 201303,
"yield": 1001, <== inserted a new attribute
}
},
"_id" : "pl"
}
I have tried this,
db.my_collection.update({_id: 'pl'},{$set: {'S.500209.yield': 1}})
But I couldn't make it. And I searched in stack overflow and google but I couldn't find out.
I got so many answers but most of them keeping sub-docuemnts in array.
Pleas help me to solve my issue, and please tell me why most of them keeping subdocuments in array.
Number key might cause the problem. Update your field name.
db.my_collection.update({_id: 'pl'},{$set: {'S.a500209.yield': 1}})
EDIT
Upgrade mongo version. It works fine with 2.4.

$all parameter in mongodb does not work with ObjectId list

I retrieve a list of ObjectId and I want to retrieve all object in my mongo database using the parameter $all
I'm using pymongo and my request look like this :
db.database.collection.find({ "_id" : { "$all" : [ObjectId('4ee371837c93dd33dc000003'),ObjectId('4eef9f647c93dd1a90000000')] } })
but the cursor count returned by the request is 0
but when I do this request:
db.database.collection.find_one({ "_id" : ObjectId('4ee371837c93dd33dc000003')})
It returns me the good object
Anyone know why it does not work?
That query does not make sense. You are asking for the unique and single-valued _id field to have all of two distinct values at the same time.
I think you want $in:
db.database.collection.find({ "_id" : {
"$in" :
[ObjectId('4ee371837c93dd33dc000003'),
ObjectId('4eef9f647c93dd1a90000000')] } })