I'm trying to get all the distinct values of some collection, I can get the result by executing db.$cmd.findOne({distinct: collection_name, key: some_key}) from within the shell. However, when I do:
mongo:do(safe, master, DbConn, some_db,
fun() ->
mongo:command(
{
distinct, some_collection,
key, some_key
}
)
end
)
I always get a blank list. I'm working on Ubuntu 12.04 + MongoDb 2.2.1 + Erlang R15B02.
Thanks!
Found the solution. It's the problem of bson-erlang as symbol is deprecated according to the bson specs.
See HERE for more info.
mongo:do(safe, master, DbConn, some_db,
fun() ->
mongo:command(
{
'distinct'=>'some_collection',
'key'=> 'some_key'
}
)
end
)
I am using the command in my Lithium project. It gives me the result.
Users::connection()->connection->command(array(
'distinct' => 'users',
'key' => 'status',
));
So the above should work for you too...
Related
is there a way to get data from a database in postgres using wasm?. I'd tried to get it using a library in rust but I got some errore when I build the package using "wasm-pack building--target web". The idea is to build a function in lib.rs file that return data from a db. I have the below code inside lib.rs:
use postgres::{Client, Error, NoTls};
use wasm_bindgen::prelude::*;
...
struct Author {
_id: i32,
name: String,
}
#[wasm_bindgen]
pub fn select_name(name: &String) -> Result<(), Error> {
let mut client = Client::connect("postgresql://user:1234#localhost:5432/db", NoTls)?;
for row in client.query(
"SELECT id, name FROM author WHERE name = $1",
&[&name],
)? {
let author = Author {
_id: row.get(0),
name: row.get(1),
};
println!(
"Select_Name => Author {} :",
author.name
);
}
Ok(())
}
but I get some errors:
error[E0432]: unresolved import `crate::sys::IoSourceState`
error[E0432]: unresolved import `crate::sys`
...
It is not possible directly (as Njuguna Mureithi rightly wrote) but it can be circumvented.
We can use the project: https://github.com/PostgREST/postgrest
and expose the API to our sql server.
We download the latest version of postgrest
https://github.com/PostgREST/postgrest/releases/tag/v9.0.0
in case of Linux we unpack
tar -xf postgrest-v9.0.0-linux-static-x64.tar.xz
then run help
./postgrest -h
create a configuration file for ./postgrest
postgrest -e > cfg.psqlrest
change the user and password for the database in the configuration file.
e.g. with
db-uri = "postgres://user:pasword#localhost:5432/dbname"
to your dbname database access user:pasword configuration
db-uri = "postgres://postgres:zaqwsxc#localhost:5432/dbname"
and run the server which will issue the api to our postgres
./postgrest cfg.psqlrest
The address http://localhost:3000 will start accessing the database dbname, which must be created in the database server beforehand.
Here is a description of the libraries needed to call the query using the API.
https://rustwasm.github.io/wasm-bindgen/examples/fetch.html
examples of API
https://postgrest.org/en/stable/api.html
I try to use the mongodb plugin as input for logstash.
Here is my simple configuration:
input {
mongodb {
uri => 'mongodb://localhost:27017/testDB'
placeholder_db_dir => '/Users/TEST/Documents/WORK/ELK_Stack/LogStash/data/'
collection => 'logCollection_ALL'
batch_size => 50
}
}
filter {}
output { stdout {} }
But I'm facing a "loop issue" probably due to a field "timestamp" but I don't know what to do.
[2018-04-25T12:01:35,998][WARN ][logstash.inputs.mongodb ] MongoDB Input threw an exception, restarting {:exception=>#TypeError: wrong argument type String (expected LogStash::Timestamp)>}
With also a DEBUG log:
[2018-04-25T12:01:34.893000 #2900] DEBUG -- : MONGODB | QUERY | namespace=testDB.logCollection_ALL selector={:_id=>{:$gt=>BSON::ObjectId('5ae04f5917e7979b0a000001')}} flags=[:slave_ok] limit=50 skip=0 project=nil |
runtime: 39.0000ms
How can I parametrize my logstash config to get my output in the stdout console ?
It's because of field #timestamp that has ISODate data type.
You must remove this field from all documents.
db.getCollection('collection1').update({}, {$unset: {"#timestamp": 1}}, {multi: true})
I've recently re-installed Mongodb switching from the enterprise to the community version. Nevertheless, when I start mongo, this is the prompt format I get:
MongoDB Enterprise >
How can I change it to the standard prompt version? (i.e., > if I am not wrong)
You can change the prompt from within a shell session by setting the prompt variable. For example, issuing the following command within a Mongo shell ...
var prompt="this_prompt >"
... will result in the shell's prompt changing to:
this_prompt >
You can change the default prompt for all future sessions by updating your .mongorc.js (you'll find this in your $HOME directory and if it does not exist then just create it). The following addition to your .mongorc.js ...
var prompt=function() {
return ISODate().toLocaleTimeString() + " > ";
}
... will result in this prompt:
:~/dev/tools/mongodb/mongodb-osx-x86_64-3.4.7/bin$ ./mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
16:09:43 >
16:09:57 >
Or, to get this prompt: > just add the following to your .mongorc.js:
var prompt=">"
There remains the sub text to your question, namely; where is the existing MongoDB Enterprise > prompt coming from?. It's hard to say but ...
Perhaps you have a Global mongorc.js; have a look in /etc/mongorc.js if you are on a *nix system or %ProgramData%\MongoDB if you are on Windows
Mongo behavour when finding a rc file is also subject to environment variables, more details in the docs.
When you search the internet for this topic then often you find the function below. If you like to include the existing prompt then use function defaultPrompt(), e.g.
prompt = function() {
if (typeof db == 'undefined')
return '(nodb)> ';
// Check the last db operation
try {
db.runCommand( {getLastError:1} );
} catch (e) {
print(e);
}
var user = db.runCommand({connectionStatus : 1}).authInfo.authenticatedUsers[0]
if (user) {
return user.user + "#" + db + " " + defaultPrompt();
} else {
return "(anonymous)#" + db + " " + defaultPrompt();
//return db + " " + defaultPrompt(); // if you prefer
}
}
Prompts will be like this:
admin#mip mongos>
mip shard001:ARBITER>
admin#mip configRepSet:PRIMARY>
I have a problem with puppet and facter.
puppet --version -> 3.8.6
facter --version -> 2.4.6
I write a fact for softwareinventory with returnvalue a nested hash
like this:
apps = {
'Notepad++' => {
'Displayname' => 'Notepad++'
'Displayversion' => '6.8.3'
}
'Puppet(64-bit)' => {
'Displayname' => 'Puppet(64-bit)'
'Displayversion' => '3.8.6'
}
}
The puppet.conf on the client and on server have included:
stringify_facts = false
If i test on the client with a local manifest test.pp with content:
notify {$::packages['Notepad++']['Displayversion']
puppet apply test.pp
i get the right value = 6.8.3
facter -p packages returns a hash like expected.
but with a puppetrun:
puppet agent -t -d -v
Error: Could not retrieve catalog from remote server: Error 400 on SERVER:
packages is not a hash or array when accessing it with ....
If i curl the puppetdb:
curl -k http://puppetdbname:8080/v3/nodes/nodename/facts/packages
i can see only a string !
Can anyone help please. Any ideas?
Thanks
Taner
i found out puppetdbv3 show the facts always as string and puppetdbv4 do not.
I tested again on my puppetserver and i could access to my hash -> packages
normally.
regards
Taner
I have thought fastmod specifies some operations like update-in-place.
In my app I'm doing update by _id using '$' modifiers, for example:
$colleciton->update(
array('_id' => $id),
array(
'$inc' => array('hits' => new MongoInt32(1)),
'$set' => array(
'times.gen' => gettimeofday(true),
'http.code' => new MongoInt32(200)
)
),
array('safe'=>false,'multiple'=>false,'upsert'=>false)
);
I've got such logs:
Wed Jul 25 11:08:36 [conn7002912] update mob.stat_pages query: { _id: BinData } update: { $inc: { hits: 1 }, $set: { times.gen: 1343203715.684896, http.code: 200 } } nscanned:1 nupdated:1 keyUpdates:0 locks(micros) w:342973 342ms
In logs as you can see I don't have any "fastmod" flags. There is no "moved" flag, because I set fields 'times.gen' and 'http.code' on insert, so padding factor is 1.0.
Am I doing something wrong, or I misunderstood meaning of fastmod?
You are correct that "fastmod" in the logs means an in-place update. Some possible reasons for the omission of logged fastmod/in-place operations:
You are actually setting or incrementing a field that doesn't exist, so it must be added, not an in place operation
The logs only show slow queries (default >100ms), so the in-place ones are probably happening too fast to be logged
You seem to be using 2.1 or 2.2 judging by the log - did the messages disappear if/when you switched to the new version?
In terms of looking into this further:
Have a look at the profiler, try with different settings, note: profiling adds load - so use carefully.
You can also try setting the slowms value lower, either on start up or:
> db.setProfilingLevel(0,20) // slow threshold=20ms