Update in MongoDB Meteor - mongodb

Is there any scenario that explains why the update is not working, I can't seem to pinpoint the cause and I don't see any errors whatsoever. Is there a way to check the output of the update function because currently the update is not doing anything. That means the last log line shows a value different than 20170615-7702.
Db.find().forEach(function(item){
console.log(item._id+ " =======> " + item.build.parameters.BUILD_NUM);
Db.update({"_id":item._id}, {$set:{"build.parameters.BUILD_NUM":"20170615-7702"}});
console.log(Db.findOne({"_id": item._id}).build.parameters.BUILD_NUM);});
Thank you

The code above should work. I think that the problem above is that it is not able to update the document as fast as it prints the output. First try:
var itemUpdated = Db.findOne({"_id": item._id});
console.log(itemUpdated._id)
The other option is very simple go to the command line and run meteor mongo. Then see all the entries in the collection and their properties.
Third and maybe the best option to test here is to use setTimeout() for the console.log().
Hope it helps.

Related

How to make the result displayed as multiply line NOT one line for mongodb [duplicate]

Is there a way to tell Mongo to pretty print output? Currently, everything is output to a single line and it's difficult to read, especially with nested arrays and documents.
(note: this is answer to original version of the question, which did not have requirements for "default")
You can ask it to be pretty.
db.collection.find().pretty()
You can add
DBQuery.prototype._prettyShell = true
to your file in $HOME/.mongorc.js to enable pretty print globally by default.
(note: this is answer to the updated question)
You can just do this on the CLI:
echo DBQuery.prototype._prettyShell = true >> ~/.mongorc.js
And it's always going to output pretty results.
Since it is basically a javascript shell, you can also use toArray():
db.collection.find().toArray()
However, this will print all the documents of the collection unlike pretty() that will allow you to iterate.
Refer: http://docs.mongodb.org/manual/reference/method/cursor.toArray/
Oh so i guess .pretty() is equal to:
db.collection.find().forEach(printjson);
Give a try to Mongo-hacker(node module), it alway prints pretty.
https://github.com/TylerBrock/mongo-hacker
More it enhances mongo shell (supports only ver>2.4, current ver is 3.0), like
Colorization
Additional shell commands (count documents/count docs/etc)
API Additions (db.collection.find({ ... }).last(), db.collection.find({ ... }).reverse(), etc)
Aggregation Framework
I am using for while in production env, no problems yet.
Got to the question but could not figure out how to print it from externally-loaded mongo. So:
This works is for console: and is prefered in console, but does not work in external mongo-loaded javascript:
db.quizes.find().pretty()
This works in external mongo-loaded javscript:
db.quizes.find().forEach(printjson)
Check this out:
db.collection.find().pretty()

mongodb looping collection + save, objects returned several times

I'm writing a pretty big migration and had this code (coffeescript):
db.users.find().forEach (user)->
try
#some code changing the user depending on the old state
db.users.save(user)
print "user_ok: #{user._id}"
catch error
print "user_error: #{user._id}, error was: #{error}"
Some errors occured. But they occured on already processed users:
user_ok: user_1234
#many logs
user_error: user_1234 ...
How come the loop takes already processed objects?
I ended up doing:
backup = { users: [] }
db.users.find().forEach (user)->
try
#some code changing the user depending on the old state
backup.users.push user
print "user_ok: #{user._id}"
catch error
print "user_error: #{user._id}, error was #{error}"
#loop backup and save
And it works nice now, but it seems really weird. What's the point behind all that please?
When you modify an object, it might be moved by the database. The database needs to take additional care to remember which objects have been visited already. This feature is called snapshotting, you can ask for a snapshotted query using
db.collection.find().snapshot()
However, even this doesn't make guarantees about objects that were inserted or deleted during the cursor iteration. A few more caveats are explained in the link to the documentation.
Another option is to perform an $orderby on an invariable unique index. Ideally, that index is also monotonic, so if you are using ObjectIds as primary keys then the _id field comes in pretty handy, like
db.collection.find().sort({"_id" :1});

Pretty print in MongoDB shell as default

Is there a way to tell Mongo to pretty print output? Currently, everything is output to a single line and it's difficult to read, especially with nested arrays and documents.
(note: this is answer to original version of the question, which did not have requirements for "default")
You can ask it to be pretty.
db.collection.find().pretty()
You can add
DBQuery.prototype._prettyShell = true
to your file in $HOME/.mongorc.js to enable pretty print globally by default.
(note: this is answer to the updated question)
You can just do this on the CLI:
echo DBQuery.prototype._prettyShell = true >> ~/.mongorc.js
And it's always going to output pretty results.
Since it is basically a javascript shell, you can also use toArray():
db.collection.find().toArray()
However, this will print all the documents of the collection unlike pretty() that will allow you to iterate.
Refer: http://docs.mongodb.org/manual/reference/method/cursor.toArray/
Oh so i guess .pretty() is equal to:
db.collection.find().forEach(printjson);
Give a try to Mongo-hacker(node module), it alway prints pretty.
https://github.com/TylerBrock/mongo-hacker
More it enhances mongo shell (supports only ver>2.4, current ver is 3.0), like
Colorization
Additional shell commands (count documents/count docs/etc)
API Additions (db.collection.find({ ... }).last(), db.collection.find({ ... }).reverse(), etc)
Aggregation Framework
I am using for while in production env, no problems yet.
Got to the question but could not figure out how to print it from externally-loaded mongo. So:
This works is for console: and is prefered in console, but does not work in external mongo-loaded javascript:
db.quizes.find().pretty()
This works in external mongo-loaded javscript:
db.quizes.find().forEach(printjson)
Check this out:
db.collection.find().pretty()

After querying DB I can't print data as well as text anymore to browser

I'm in a web scripting class, and honestly and unfortunately, it has come second to my networking and design and analysis classes. Because of this I find I encounter problems that may be mundane but can't find the solution to it easily.
I am writing a CGI form that is supposed to work with a MySQL DB. I can insert and delete into the DB just fine. My problem comes when querying the DB.
My code compiles fine and I don't get errors when trying to "display" the info in the DB through the browser but the data and text doesn't in fact display. The code in question is here:
print br, 'test';
my $dbh = DBI->connect("DBI:mysql:austinc4", "*******", "*******", {RaiseError => 1} );
my $usersstatement = "select * from users";
my $projstatment = "select * from projects";
# Get the handle
my $userinfo = $dbh->query($usersstatement);
my $projinfo = $dbh->query($projstatement);
# Fetch rows
while (#userrow = $userinfo->fetchrow()) {
print $userrow[0], br;
}
print 'end';
This code is in an if statement that is surrounded by the print header, start_html, form, /form, end_html. I was just trying to debug and find out what was happening and printed the statements test and end. It prints out test but doesn't print out end. It also doesn't print out the data in my DB, which happens to come before I print out end.
What I believe I am doing is:
Connecting to my DB
Forming a string the contains the command/request to the DB
Getting a handle for my query I perform on the DB
Fetching a row from my handle
Printing the first field in the row I fetched from my table
But I don't see why my data wouldn't print out as well as the end text. I looked in DB and it does in fact contain data in the DB and the table that I am trying to get data from.
This one has got me stumped, so I appreciate any help. Thanks again. =)
Solution:
I was using a that wasn't supported by the modules I was including. This leads me to another question. How can I detect errors like this? My program does in fact compile correctly and the webpage doesn't "break". Aside from me double checking that all the methods I do use are valid, do I just see something like text not being displayed and assume that an error like this occurred?
Upon reading the comments, the reason your program is broken is because query() does not execute an SQL query. Therefore you are probably calling an undefined subroutine unless this is a wrapper you have defined elsewhere.
Here is my original posting of helpful hints, which still apply:
I hope you have use CGI, use DBI, etc... and use CGI::Carp and use strict;
Look in /var/log/apache2/access.log or error.log for the bugs
Realize that the first thing a CGI script prints MUST be a valid header or the web server and browser become unhappy and often nothing else displays.
Because of #3 print the header first BEFORE you do anything, especially before you connect to the database where the script may die or print something else because otherwise the errors or other messages will be emitted before the header.
If you still don't see an error go back to #2.
CGIs that use CGI.pm can be run from a command line in a terminal session without going through the webserver. This is also a good way to debug.

Difference between 'Execute' and 'Execute as script'

I'm currently experiencing what I believe is strange behavior when using Oracle with TOAD.
I have a query:
SELECT
COUNT(as_at_date)
FROM
job_log
WHERE
as_at_date = TO_DATE('24/11/2009', 'dd/mm/yyyy');
When I try to run this using Toads 'Execute statement' button I get zero rows returned. However when I use 'Execute as script' I get one row returned, which is as expected.
Can anyone explain to me why using the different commands would produce differences in the result set?
Thanks.
It's not an direct answer to your question (and it will take you a moment or two to setup and analyse), but you could take a TKPROF trace in the database to see what actually arrives from Toad....
See
http://www.jlcomp.demon.co.uk/tkprof_01.html
and
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:969160000346108326
for some examples.
Execute will run the query where the cursor is present.
Execute as script will execute all the queries written in that tab from top to bottom.
Probably when pressed "EXECUTE" button the cursor would have been in an empty line.