MongoDB:createCollection with parameters - mongodb

I used this comment
> db.createCollection("naveen",{capped:true,autoIndexId:true,size:53440099,max:1000});
and I got this:
{
"note" : "the autoIndexId option is deprecated and will be removed in a future release",
"ok" : 1
}

In fact, MongoDB doesn't need to create a collection, when you insert document MongoDB will automatically create a collection for you, and also generate unique Id for each document!
Example:
db.createCollection("naveen",{capped:true,autoIndexId:true,size:53440099,max:1000}); //don't
you don't need to create a collection, you just need to insert a direct document, it's not like SQL, It's JSON.
{ name: 'john', ...}
like this, insert document without creating a collection, it automatically generates a collection for you!
db.newYear.insert({ name: 'john', age: 33, year: 2020 });

Removing the autoIndexId parameter will remove the note in the response.
From MongoDB version 3.2, the autoIndexId parameter is deprecated when using createCollection, hence you are receiving this note message along with the ok value to make you aware of this.
The autoIndexId parameter is removed in version 3.4.
A reply to a comment below is useful in this answer:
and replaced with what?
Looking at this SO answer, MongoDB docs and MongoDB's JIRA it seems they are pushing for developers to not intervene with auto indexing.

Related

Meteor React - Why is findOne on a single document not found in miniMongo when it does exist?

This is such a weird problem. I think it has to do with how I am querying the document. It seems like the Meteor API has changed to query documents but the docs on the website are the same.
Here is a document in the database:
meteor:PRIMARY> db.studies.findOne()
{ "_id" : ObjectId("56c12e6537014a66b16771e7"), "name" : "Study 1" }
I have subscribed to get all documents and here is what I am trying in the console to get the documents.
var study = Studies.findOne() // This works.
It returns:
_id: MongoID.ObjectID
_str: "56c12e6537014a66b16771e7"
name: 'Study 1'
I just started a new Meteor project with React. I see that my collection is returning _id: MongoId.ObjectId
This is different, I have been using Meteor for awhile with Blaze and I can't remember it returning MongoID.ObjectID instead of just the string
But now if I try and find just that one document, it does not work.
var study = Studies.findOne("56c12e6537014a66b16771e7");
or
var study = Studies.findOne({_id: "56c12e6537014a66b16771e7"});
I am positive I am queuing for the right _id field. I have double checked the ID. Why does trying to find this one document not work?
Please let me know how I can query for a document. Has something changed with Meteor? The documentation still says you can search by id string.
You need to explicitly cast object id string to an ObjectID
var study = Studies.findOne({_id: new Meteor.Collection.ObjectID("56c12e6537014a66b16771e7")});
#Jaco has the correct answer, but I wanted to answer here to clarify what the higher level issue was.
The reason why my find query was not following syntax in Meteor docs is because I inserted the document into MongoDB directly, instead of through the Meteor API.
If you insert the document directly into MongoDB, you have to query the document using the syntax #Jaco mentioned in his answer.
Similar question: Meteor - Find a document from collection via Mongo ObjectId
So instead of changing my query code, I just deleted the document I inserted directly into MongoDB, and inserted a documented using the console in the browser.
Now I can query the document like normal.
So the root of the issue is that if you insert the document directly into MongoDB, you don't get the same type of document as you would if you insert the document using the Meteor API.

How to overwrite object Id's in Mongo db while creating an App in Sails

I am new to Sails and Mongo Db. Currently I am trying to implement a CRUD Function using Sails where I want to save user details in Mongo db.In the model I have the following attributes
"id":{
type:'Integer',
min:100,
autoincrement:true
},
attributes: {
name:{
type:'String',
required:true,
unique:true
},
email_id:{
type:'EMAIL',
required:false,
unique:false
},
age:{
type:'Integer',
required:false,
unique:false
}
}
I want to ensure that the _id is overridden with my values starting from 100 and is auto incremented with each new entry. I am using the waterline model and when I call the Api in DHC, I get the following output
"name": "abc"
"age": 30
"email_id": "abc#gmail.com"
"id": "5587bb76ce83508409db1e57"
Here the Id given is the object Id.Can somebody tell me how to override the object id with an Integer starting from 100 and is auto incremented with every new value.
Attention: Mongo id should be unique as possible in order to scale well. The default ObjectId is consist of a timestamp, machine ID, process ID and a random incrementing value. Leaving it with only the latter would make it collision prone.
However, sometimes you badly want to prettify the never-ending ObjectID value (i.e. to be shown in the URL after encoding). Then, you should consider using an appropriate atomic increment strategy.
Overriding the _id example:
db.testSOF.insert({_id:"myUniqueValue", a:1, b:1})
Making an Auto-Incrementing Sequence:
Use Counters Collection: Basically a separated collection which keeps track the last number of the sequence. Personally, I have found it more cohesive to store the findAndModify function in the system.js collection, although it lacks version control's capabilities.
Optimistic Loop
Edit:
I've found an issue in which the owner of sails-mongo said:
MongoDb doesn't have an auto incrementing attribute because it doesn't
support it without doing some kind of manual sequence increment on a
separate collection or document. We don't currently do this in the
adapter but it could be added in the future or if someone wants to
submit a PR. We do something similar for sails-disk and sails-redis to
get support for autoIncremeting fields.
He mentions the first technique I added in this answer:
Use Counters Collection. In the same issue, lewins shows a workaround.

create new collection in mongodb by adding index in system.indexes

when I attempt to insert new document manually in system.indexes collection in mongodb,new collection created.here goes the code
{
"v" : 1,
"key" : {
"code" : 1
},
"name" : "code_1",
"ns" : "mydb.collection"
}
where collection is my collection name which is not already present in database and mydb is my database name. Why new collection is getting created?
Is it possible to create collection by adding index manually in system.indexes.
Why are you asking us this? You already tried to add a new index to system.indexes. Has a new collection been created? If yes, then yes it is possible, if no, then not possible.
Is this a correct way?
How do you think? Have you read somewhere in documentation that in order to create a new collection you need to dance around and to create manually indexes in some system defined collection? Or may be it was written in documentation that db.createCollection(name, options) is what you should do or if you so desire you can just insert a document in a non existed collection and it will create it.
So why after all this one might think that the correct way is to do some manipulation with system.indexes?
As a complement to #Salvador Dali's answer strongly discouraging you to do modify system.index directly: if for some reason you really don't want/can't use createCollection, just remember this is a wrapper around the create command.
You can issue yourself such command to create a new collection:
db.runCommand( { create: "collection" } )
As about inserting an entry in system.indexes: from the doc:
Deprecated since version 3.0: Access this data using listIndexes.
The <database>.system.indexes collection lists all the indexes in the database.
By reading that it appears that system.indexes should be considered as read-only (its direct use is even deprecated since 3.0). The behavior you observed should be considered as unspecified. And so unreliable and subject to change without further notices.
If you really need to understand why it behave that way, maybe you should take a look at the source code or ask the question on the MongoDB developer mailing list. There you could have all the insights.

Mongodb update. setOnInsert Mod on _id not allowed

I understand the fact that you can't update _id on an existing mongodb document.
But is there a reason that we can't use it in an upsert in the 'setOnInsert' part ? Because it is 'on insert' so it's not an update.
My expected usage is this:
db.myCol.update({_id:12345},{$setOnInsert:{_id:12345},$set:{myValue:'hi'}});
Is this a bug or am i missing something ?
MongoDB uses the 'query' part for an upsert query as part of the set, meaning that you don't have to specify the _id in the set part of you want to specify your own _id.
note: my query above also had a small bug which was the missing upsert flag.
This is the correct query:
db.myCol.update({_id:12345},{$set:{myValue:'hi'}},{upsert:true});
If the record doesn't exist, this query will insert a record which looks like this:
{_id:12345,myValue:'hi'}
Really, this is a bug in mongo fixed in development version 2.5.4 or release 2.6.0:
https://jira.mongodb.org/browse/SERVER-9958
Once that's fixed, I believe this should work as desired. In the general case, the query for update won't necessarily specify an "_id" field.

Aggregation: Project dotted field doesn't seem to work

I have a database containing this document:
{"_id":{"$id":"xxx"},"duration":{"sec":137,"usec":0},"name":"test"}
If I call db.collection.aggregate with this pipeline:
{$project:{_id: 0, name: 1, duration: 1, seconds: "$duration.sec"}}
I get this result:
{"result":[{"duration":{"sec":137,"usec":0},"name":"test"}],"ok":1}
Why does the result not have a 'seconds' field? Have I used the wrong projection syntax?
I'm not entirely sure of the version of mongodb the server is running. I'm using the 1.3.1 php driver with php 5.4.3, but the server may be older than that - perhaps by about half a year?
According to the MongoDB documentation on $project:
You may also use $project to rename fields. Consider the following
example:
db.article.aggregate(
{ $project : {
title : 1 ,
page_views : "$pageViews" ,
bar : "$other.foo"
}} );
This operation renames the pageViews field to page_views, and renames the foo field in the other sub-document as the top-level
field bar.
That example seems to match-up pretty good with what you are trying to do.
I know 10gen officially released the aggregation framework with MongoDB v2.2. Check out the current production release, which I believe is 2.2.3. If you are running on a prior development version, there could be something odd going on with aggregation.
As Bryce said, I'm currently using MongoDB 2.6 through the shell and the $project pipeline is working for renaming nested fields as you do.
db.article.aggregate({$project:{'_id': 0, 'name': 1, 'duration': 1, 'seconds': '$duration.sec'}}
I've not tried yet trough the python or php drivers but my former pipelines with the last pymongo worked very well.