using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Collections;
using
MongoDB;
using
MongoDB.Linq;
using
MongoDB.Attributes;
namespace
Demo
{
class
Program
{
public
class
Person
{
[MongoAlias(
"
fn
"
)]
//
标记此属性存入MongoDB中的字段名
public
string
FirstName {
get
;
set
; }
[MongoAlias(
"
ln
"
)]
public
string
LastName {
get
;
set
; }
[MongoAlias(
"
age
"
)]
public
int
Age {
get
;
set
; }
[MongoAlias(
"
add
"
)]
public
Address PrimaryAddress {
get
;
set
; }
[MongoAlias(
"
otherAdds
"
)]
public
List
<
Address
>
Addresses {
get
;
set
; }
[MongoAlias(
"
emps
"
)]
public
int
[] EmployerIds {
get
;
set
; }
public
string
MidName {
get
;
set
; }
public
Oid LinkedId {
get
;
set
; }
}
public
class
Address
{
[MongoAlias(
"
city
"
)]
public
string
City {
get
;
set
; }
public
bool
IsInternational {
get
;
set
; }
public
AddressType AddressType {
get
;
set
; }
}
public
enum
AddressType
{
Company,
Private
}
public
class
PersonWrapper
{
public
Person Person {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
PersonWrapper(Person person,
string
name)
{
Person
=
person;
Name
=
name;
}
}
static
void
Main(
string
[] args)
{
Mongo mongo
=
new
Mongo(
"
Server=127.0.0.1:27017
"
);
mongo.Connect();
IMongoDatabase simple
=
mongo[
"
simple
"
];
var Collection
=
simple.GetCollection
<
Person
>
(
"
people
"
);
//
var DocumentCollection = simple.GetCollection("people");
Collection.Insert(
new
Person
{
FirstName
=
"
Bob
"
,
LastName
=
"
McBob
"
,
Age
=
42
,
PrimaryAddress
=
new
Address { City
=
"
London
"
},
Addresses
=
new
List
<
Address
>
{
new
Address { City
=
"
London
"
},
new
Address { City
=
"
Tokyo
"
},
new
Address { City
=
"
Seattle
"
}
},
EmployerIds
=
new
[] {
1
,
2
}
},
true
);
Collection.Insert(
new
Person
{
FirstName
=
"
Jane
"
,
LastName
=
"
McJane
"
,
Age
=
35
,
PrimaryAddress
=
new
Address { City
=
"
Paris
"
},
Addresses
=
new
List
<
Address
>
{
new
Address { City
=
"
Paris
"
}
},
EmployerIds
=
new
[] {
1
}
},
true
);
Collection.Insert(
new
Person
{
FirstName
=
"
Joe
"
,
LastName
=
"
McJoe
"
,
Age
=
21
,
PrimaryAddress
=
new
Address { City
=
"
Chicago
"
},
Addresses
=
new
List
<
Address
>
{
new
Address { City
=
"
Chicago
"
},
new
Address { City
=
"
London
"
}
},
EmployerIds
=
new
[] {
3
}
},
true
);
mongo.Disconnect();
mongo.Dispose();
Console.ReadLine();
}
}
}