我正在研究一种可以从 json 输入运行 mongodb 聚合查询的服务方法。这个想法是您将使用 Builders 生成查询,将该查询转换为 json,将其传递给要反序列化并运行的服务。对于查找查询,我可以像这样使用 Bson 文档
public string DoGenericFind(string queryDoc, string collectionName)
{
BsonDocument document = BsonSerializer.Deserialize<BsonDocument>(queryDoc);
var results = _context.Database.GetCollection<dynamic>(collectionName).FindSync<BsonDocument>(document);
if (results == null)
return null;
else
return results.ToList().ToJson();
}
Run Code Online (Sandbox Code Playgroud)
我很难找到类似的方法来使用聚合来做到这一点。我发现的唯一示例尝试执行与此类似的操作,它们传递某种 BsonDocument[]。但是,我的驱动程序版本(2.5)的智能感知说我需要传递一个管道定义,我找不到如何使用的好例子。
我正在构建一个简单的角度应用程序,我想要一个服务来保持一些状态.我第一次尝试这只是一个布尔记录.基本上,我想要一个导航栏组件来订阅这个值,这样当它为false时它会显示一个'login'选项,当它的true显示一个'admin'按钮时.
我的服务:
@Injectable()
export class AuthService {
private _loggedIn = new BehaviorSubject(false);
public readonly loggedIn$ = this._loggedIn.asObservable();
constructor(private http: Http, private router: Router) {
this._loggedIn = new BehaviorSubject(false);
}
public login(u: User): Observable<any> {
let obs = this.http.post(ApiRoutes.login, u).map(res => res.json());
obs.subscribe(res => {
this._loggedIn.next(res.success);
if (this._loggedIn.getValue())
this.router.navigate(['/admin']);
});
return obs;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的导航组件中:
@Component({
selector: 'app-nav',
templateUrl: './nav.component.html',
styleUrls: ['./nav.component.scss'],
providers: [AuthService]
})
export class NavComponent implements OnInit {
loggedIn: boolean;
constructor(private as: AuthService) { }
ngOnInit() {
this.as.loggedIn$.subscribe(isLoggedIn …Run Code Online (Sandbox Code Playgroud)