How to create a user for Mongdb?

To create a user in mongdb, you need to open a terminal, assuming mongod service is running, ,

    (This command will do the job:   brew services start mongodb)

   >mongo
   >use testdb

  db.createUser(
  {
    user: "testUser",
    pwd: "12345678",
    roles: [
       { role: "read", db: "reporting" },
       { role: "read", db: "products" },
       { role: "read", db: "sales" },
       { role: "readWrite", db: "accounts" }
    ]
  }
)



</pre>
<div class="line number4 index3 alt1"><code class="plain plain">db.createUser(</code></div>
<div class="line number5 index4 alt2"><code class="plain spaces">  </code><code class="plain plain">{</code></div>
<div class="line number6 index5 alt1"><code class="plain spaces">    </code><code class="plain plain">user: "root",</code></div>
<div class="line number7 index6 alt2"><code class="plain spaces">    </code><code class="plain plain">pwd: "DanielSecret2022",</code></div>
<div class="line number8 index7 alt1"><code class="plain spaces">    </code><code class="plain plain">roles: [</code></div>
<div class="line number9 index8 alt2"><code class="plain spaces"></code> <code class="plain plain"></code></div>
<div class="line number11 index10 alt2"><code class="plain spaces">       </code><code class="plain plain">{ role: "read", db: "RNAseqAnalysis" },</code></div>
<div class="line number12 index11 alt1"><code class="plain spaces">       </code><code class="plain plain">{ role: "readWrite", db: "RNAseqAnalysis" }</code></div>
<div class="line number13 index12 alt2"><code class="plain spaces">    </code><code class="plain plain">]</code></div>
<div class="line number14 index13 alt1"><code class="plain spaces">  </code><code class="plain plain">}</code></div>
<div class="line number15 index14 alt2"><code class="plain plain">)</code></div>
<div class="line number15 index14 alt2"></div>
<div class="line number15 index14 alt2"></div>
<pre>

Try to connect to it in you code with mongodb://testUser:12345678@yourhost:port/testdb

Enjoy!

Read More

How to sort a List with Java Lambda

want to sort a list in java with Lambda? Here is how:

 
class Person {
	private Integer age;
	private String name;

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Person(Integer age, String name) {
		super();
		this.age = age;
		this.name = name;
	}

	@Override
	public String toString() {
		return "Person [age=" + age + ", name=" + name + "]";
	}
}

 
package com.leespace.java8;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class JavaLambdaSort {

	public static void main(String[] args) {

		 List <Person> people= getArrayList();
		 
		 //Sort by name  in traditional way with Comparator:
		 Collections.sort(people, new Comparator <Person> () {

			@Override
			public int compare(Person p1, Person p2) {
				return p1.getName().compareTo(p2.getName());
			}
		 });
		 displayList(people);
		 
		 //Sort by name  in traditional way with Comparator:
		 Collections.sort(people, new Comparator <Person> () {

			@Override
			public int compare(Person p1, Person p2) {
				return p1.getAge().compareTo(p2.getAge());
			}
		 });
		 displayList(people);
		 
		 //Sort by name  in with lambda:
		 Collections.sort(people, (p1,p2) -> p1.getName().compareTo(p2.getName()));
		 displayList(people);
		 
		 //Sort by age  in with lambda:
		 Collections.sort(people, (p1,p2) -> p1.getAge().compareTo(p2.getAge()));
		 displayList(people);
		 
	}

	private static void displayList(List<Person> people) {
		System.out.println("\nThe sorted list:");
		for (Person p:people) {
			 System.out.println(p);
		 }
	}

	private static List<Person>   getArrayList() {
		List<Person> people = new ArrayList<Person>();
		Person person1 = new Person(21, "Jack Lee");
		people.add(person1);
		Person person2 = new Person(31, "Jack Ma");
		people.add(person2);
		Person person3 = new Person(12, "Steve Liu");
		people.add(person3);
		Person person4 = new Person(42, "Alice XU");
		people.add(person4);
		
		return people;
	}

}


Read More

How to set up a Nginx server on Mac computer

From wiki: Nginx (pronounced “engine-x”) is an open source reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer, HTTP cache, and a web server (origin server). The nginx project started with a strong focus on high concurrency, high performance and low memory usage. It is licensed under the 2-clause BSD-like license and it runs on Linux, BSD variants, Mac OS X, Solaris, AIX, HP-UX, as well as on other *nix flavors. It also has a proof of concept port for Microsoft Windows.

 

  • Step 1: install nginx with command:
    • brew install nginx
  • step 2: Prepare for start nginx service
    • sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons
    • sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
  • step 3: You can also do some basic configuration by specifying the location file is feeding up.
    • By default, nginx is running on port 8080, so you can verify if the server is running of not by visiting :http://localhost
    • Also, if you want to stop the servie, you can run this command
    • sudo launchctl unload -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

The nginx configuration file is located in /usr/local/etc/nginx/

Here is an example for some basic setting:

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

root /var/www/virtual/test.com/htdocs;
index index.html inde.htm;

location / {
default_type “text/html”;
try_files $uri.html $uri $uri/ /index.html;
}

you need to restart nginx service to have the new setting loaded up, so with this simple change, port is changed to 80 from 8080 and you can visit your site from http://localhost.
Enjoy!

Read More